Docker Basic Tips 1: How to download an image and work with containers ?
In this post, we will look at the tips of using docker to get images and creating & working with containers.
1. Pull an image:
$ docker pull centos Using default tag: latest latest: Pulling from library/centos a02a4930cb5d: Pull complete Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426 Status: Downloaded newer image for centos:latest $
2. Checkout created/available images.
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos 7 1e1148e4cc2c 2 months ago 202MB centos latest 1e1148e4cc2c 2 months ago 202MB
3. Creating a container from image ID:
$ docker create --name centos_cont1 -p 80:80 1e1148e4cc2c
1f596e8318d40eb69e04094ae5595fd78fd675cd413ab456bfa7c73265c471da
or,
Create a running container(preferred):
$ docker run -dt --name centos_cont2 1e1148e4cc2c
b34a07319de9f92696ebef51508c551a22be4f89903214264301922d475bbe1d
4. See all the available containers.
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1f596e8318d4 1e1148e4cc2c "/bin/bash" 36 seconds ago Created centos_cont1 ## We can also use below command: $ docker container ls --all
5. Login to container:
# start the container $ docker start 42e6d6ee690e; # attach to the container, i.e. taking its console. $ docker container attach 42e6d6ee690e 42e6d6ee690e [root@42e6d6ee690e /]# ifconfig -a
6. Copying files to the container:
$ echo "Hello test file" > testfile1 $ docker cp testfile1 centos_cont1:/testfile1 $
7. To execute a command like /bin/bash on container to get the shell:
-i keep STDIN open, -t allocates a pseudo TTY. $ docker exec -it centos_cont1 /bin/bash Error response from daemon: Container 1f596e8318d40eb69e04094ae5595fd78fd675cd413ab456bfa7c73265c471da is not running ### To resolve above error, first start the container ### using "docker start" command.