Docker commands

This post is written as a study note of docker tutorial in YouTube https://www.youtube.com/watch?v=fqMOX6JJhGo&t=1182s, and also cheat sheet for future references during work.

 run - start a container

 docker run image_name 
This command will try to find the image locally in the docker host first. If it cannot find it, it will look for it in docker hub. If image is found in docker hub, it will pull the image down from there and run it. Once the image is downloaded from docker hub, the second time run will not trigger the downloading again from docker hub.

ps - list containers

 docker ps 
This command is used to list all the running containers with some basic information. It is kinda like "ls" command to list all the files in the current folder.

 docker ps -a 
This command is used to list all the running containers as well as the recent stopped containers.

inspect - inspect container

 docker inspect silly_sammet 
You can use this command to check more details of a container.

stop - stop a container

 docker ps 

First use "ps" command to get the container name.



 docker stop silly_sammet 
Then use "stop" command to stop a container.

 docker ps -a 
Then use "ps -a" command to confirm the container is stopped.

rm - remove a container

 docker rm silly_sammet 
This command will remove the container from the memory completely.

 docker ps -a 
Then use "ps -a" command to confirm the container is removed.

images - list images

 docker images 
This command is used to list the images in the docker host.

rmi - remove images

Before remove the image, make sure the container from this image is stopped.
 docker rmi docker/whalesay 
Use this command to remove the image in the host.

 docker images 
Then you use "images" command to confirm the deletion.

pull - download an image

 docker pull docker/whalesay 
This command can be used to download docker image from docker hub directly, unlike docker run command to check the image locally then go search in docker hub. However this command will not run the image after downloading.

exec - execute a command

"exec" command is used to execute commands in a container. For example, if a ubuntu container is running, you can use "exec" to execute any linux command in this ubuntu container.
 docker ps -a 
Use "ps" command to get the container name. 



 docker exec nifty_bouman cat /etc/hosts 
 Then execute "cat /etc/hosts" with container name nifty_bouman to show the content of file hosts.








run - attach and detach

If you run a container using "docker run ubuntu", it runs a container with "attach" mode, so you cannot do anything except watching the output from the container.
However if you run container with "dettach" mode, you can still use the console with the container running at the background.
  docker run -d ubuntu sleep 100 
The output is:



However in "dettach" mode, you cannot see the log information from the console anymore since it runs in the background. But no worries, you can use below command to see the log:
 docker logs nifty_bouman 

You can use "attach" command to attach the console to a running container.
 docker attach 8ae0d 
You can just provide few characters of the container ID like above. No need to give the full ID.

run - port mapping

 docker run -p 80:5000 simple-webapp 
You can use "run -p" command to map the dock container port (5000) to host port(80). Then the user can use the host IP address + host port to access the application in docker container.
By using this command, you can map different application to different ports on the host like below:
 docker run -p 8000:5000 simple-webapp1 
 docker run -p 8001:5000 simple-webapp2 
 docker run -p 8002:5000 simple-webapp3 

run - set environment variables

 docker run -e APP_COLOR=blue simple-webapp 
Use "run -e" or "run --env" to set environment variables when run a container.

run - volume mapping


Volume mapping in docker allows you to mount a directory in host into docker container. Thus whenever you want to save some data or files into that directory in container, it will actually be saved in the host. The benefit is that when you remove the container, the data is still in the host without impact from the removing.

 docker run -v /opt/datadir:/var/lib/mysql mysql 

In above example, /opt/datadir is a directory in the host, and /var/lib/mysql is a directory in the container.

You can also create a volume in the host first using "volume create" command like below:
 docker volume create data_volume 
This command will create a volume in host in path /var/lib/docker/volumes.
/var/lib/docker
----volumes
--------data_volume
Then use "run -v" command to map volume to directory in a container.
 docker run -v data_volume:/var/lib/mysql mysql 

In fact, even if the volume is not created in the beginning using "docker volume" command, "run -v" command will automatically create a volume in the host (data_volume).

Now there is a new way to map the volume using "run --mount" command, it has more parameters to specify during execution.
 docker run --mount type=bind,source=/opt/datadir,target=/var/lib/mysql mysql 

run - links

Because services are connected to each other for most of the time, we need to specify the relationship between containers when run images.
For example, we have below voting app. It has the relationship like below:

In order to describe the above connections between containers, "run --link" command is used to run containers like below:
 docker run -d --name=redis redis 
 docker run -d --name=db postgres:9.4 
 docker run -d --name=vote -p 5000:80 --link redis:redis voting-app 
 docker run -d --name=result -p 5001:80 --link db:db result-app 
 docker run -d --name=worker --link db:db --link redis:redis worker 


run - limiting resource usage on host

You can use below command to limit the CPU usage:
 docker run --cpus=.5 ubuntu # use up to 50% of the host CPU 
Similarly you can use below command to limit the memory usage:
 docker run --memory=100m ubuntu # use up to 100m bytes of the host memory 

build - build an image

 docker build MyAppDir -t mmumshad/my-custom-app 
Use "build" command to build an image. "MyAppDir" is the directory name contains docker file used to build an image. Then specify the image name after "-t".
Below is an example of docker file.
Dockerfile:
FROM Ubuntu
RUN apt-get update && apt-get -y install python
RUN pip install flask flask-mysql
COPY . /opt/source-code
ENTRYPOINT FLASK_APP=/opt/source-code/app.py flash run




Comments

Popular posts from this blog

Basic understanding of TLS-PSK protocol

Differences between ASIC, ASSP and ASIP

Orthogonal instruction set