Docker Images and Containers

Source: Internet
Author: User
Tags mkdir centos docker stop container docker ps docker run
Preface:
The relationship between the image and the container is one-to-many, namely: the same image can create different versions of the container;
Therefore, you need to create the mirror and then run the image and specify the appropriate parameters, such as: version, port, container name,

1. Create Image
Before creating the image, you need to create the Dockerfile file, which is the most important configuration file for Docker, including various configurations such as Docker run:
Example:
[Root@centost1 centos7]# VI Dockerfile
Complete the following simple script test:
From centos:centos7.1.1503
cmd ["/bin/echo", "This is Test CMD Docker"]
To perform the image creation command:
Dockerfile in the current directory, use "." Said:
[Root@centost1 centos7]# Docker build-t test/centos:7.1.
or specify the directory where Dockerfile is located:
[Root@centost1 centos7]# Docker build-t test/wordpress:4.2/usr/software/docker/git/docker-training/wordpress
Step 9:add supervisord.conf/etc/supervisord.conf
---> 805f510ea2c8
Removing intermediate container 01B28338B0CA
Step 10:run mkdir-p/etc/supervisor.conf.d && mkdir-p/var/log/supervisor
---> Running in ea323222231c
---> 0ccb19ab1472
Removing intermediate container ea323222231c
Step 11:expose 22
---> Running in 46b3fae7614a
---> bfa2da373c93
Removing intermediate container 46b3fae7614a
Step 12:entrypoint/usr/bin/supervisord-n-c/etc/supervisord.conf
---> Running in Fd69533f2fae
---> b71b18e0e65b
Removing intermediate container Fd69533f2fae
Successfully built b71b18e0e65b

Description: If you do not specify a version, the default version is latest

2. Build Container
After you create the mirror, you need to start mirroring to generate the appropriate container
To view the currently owned mirrors, you can use Docker

[Root@centost1 centos7]#  Docker images
Boot mirroring is typically initiated in two ways:
1) Docker run-d (Start container in background, return a container number)
2) Docker run-it (interactive mode starts, the container boots to the foreground)

Such as:
[Root@centost1 centos7]# Docker run-d-P 2222:22--name base test/centos:7.1
6b11b19c4828594a2710c6ecbd721c50c3a4d296187c3c3c62d396081d256e64
Description
--name: The name of the container (can be customized)
-d-p 2,222:22 (using the host's 2222 port to map to the 22 port of the Docker container)

If you start an error,
Conflict. The name "/base" is already on use by container
Remove the old container and start again.

3. View Container:
Docker PS (view running containers)
Docker ps-a (view all containers [including non-running])

[Root@centost1 centos7]# Docker PS
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6b11b19c4828 test/centos:7.1 "/usr/bin/supervisord" 2 minutes ago up 2 minutes 0.0.0.0:2222->22 /TCP Base

4. Enter the container
After the container starts, it can go inside the container for some command operations:
Example:
[Root@centost1 php-fpm]# Docker exec-it Base/bin/bash
[root@8ded2f9c63cc/]#

5, the management of the container
The container can be managed after the container is started by mirroring run:
Grammar:
Docker start container name: Start one or many containers that have been stopped
Docker Stop container name: Stop a running container
Docker Restart container name: Restart container
Docker RM container Name: Delete container
or Docker rm-f container name: Force Delete Container

Example:
[Root@centost1 ~]# Docker start base
Base

6. Container removal
First look at the image that needs to be removed:
[Root@centost1 ~]# Docker Ps-a
You can then delete the image based on the container ID:
[Root@centost1 ~]# Docker RM a1157d56ac1b
Note: If the container is already running, you need to stop the container and delete it, or use Docker rm-f to force the deletion;

7. Image deletion
Image deletion is similar to container deletion
[Root@centost1 ~]# Docker RMI test/centos:7.0
Or
[Root@centost1 ~]# Docker rmi-f test/centos:7.0

8, Docker run some parameter description:
1) Docker Run-v: Set up the Mirrored data backup directory:
Example:
[Root@centost1 mysql]# Docker run-d-P 3306:3306-v/usr/software/docker/git/mydata:/var/lib/mysql--name dbserver test/ mysql:5.5
Description
-v/usr/software/docker/git/mydata:/var/lib/mysql
Save MySQL Data directory to/usr/software/docker/git/mydata
When the container is removed, the data from the backup directory can be mounted from the new:
Example:
[Root@centost1 mysql]# Docker run-d-P 3306:3306--name newdb-v/usr/software/docker/git/mydata:/var/lib/mysql
2) Docker Run-rm
[Root@centost1 mysql]# Docker run-it--rm test/cmd:1.0
Description: (indicates that the container is deleted after execution is completed)









Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.