Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Develop Intelligence – Docker Creating Custom Docker Images

Advance Docker Commands


To view all these layers once an image is built from a Dockerfile, we can use docker history command.
docker history sandeepsoni/helloworld:v2

Docker command for inspecting the image


docker image inspect sandeepsoni/helloworld:v2
docker image inspect --format "{{json .RootFS.Layers }}" sandeep/helloworld:v2

Docker command for not removing intermediate containers while building an image using Dockerfile
docker build -t helloworld --no-cache .
//no-cache ensures that each instruction is executed and docker cache is not used.

Docker DIFF:
We can use the following command to find the difference to filesystem the dockerfile command has caused to an
image
docker diff <container id>
• A = added
• C = changed
• D = deleted

Docker TOP:
• To see the the running processes for a container, you can do this directly without logging in to an interactive
shell on a container.
docker top <container>

Docker STATS
• Useful to see what Docker processes are running on the HOST and what their resource usage is.
• Works much like the top Linux command.
docker stats <container Id>

Docker CP:
• Allows you to copy files to/from a Docker container to/from the host computer.
• Format is:
• docker cp <container name>:<path> <host path>
• docker cp <host path> <container name>:<path>

Docker KILL
Develop Intelligence – Docker Creating Custom Docker Images
• Unlike docker stop, which will attempt to stop a container process gracefully, docker kill will send a SIGKILL
signal to the container’s running process.
• This is a bit more brute force.
• Typically this isn’t needed, as a docker stop will send a SIGKILL automatically if it doesn’t shut down gracefully.

We can execute following command to note all the sequence of steps performed when a docker command is
executed.
docker events --format "{{json .}}" //Execute this in a new command prompt

Restart Policy
docker run --restart unless-stopped redis

docker update --restart unless-stopped redis

no Do not automatically restart the container. (the default)


on-failure[:max- Restart the container if it exits due to an error, which manifests as a non-zero
retries] exit code. Optionally, limit the number of times the Docker daemon attempts
to restart the container using the :max-retries option.
always Always restart the container if it stops.
unless-stopped Similar to always, except that when the container is stopped (manually or
otherwise), it is not restarted even after Docker daemon restarts.
Keep the following in mind when using restart policies:
 A restart policy only takes effect after a container starts successfully. In this case, starting successfully means
that the container is up for at least 10 seconds and Docker has started monitoring it. This prevents a container
which does not start at all from going into a restart loop.
 If you manually stop a container, its restart policy is ignored until the Docker daemon restarts or the container
is manually restarted. This is another attempt to prevent a restart loop.

Loading and Saving Images


 The docker save - Save one or more images to a tar archive (streamed to STDOUT by default)
 The docker load - Load an image from a tar archive or STDIN
$ docker save -o mynginx.tar nginx
$ docker rmi nginx
$ docker load < mynginx.tar

Docker IMPORT and EXPORT


 The docker export - Export a container’s filesystem as a tar archive
Develop Intelligence – Docker Creating Custom Docker Images
 The docker import - Import the contents from a tarball to create a filesystem image

Why import / export?


 This can be used to modify an image to a single layer.
 Useful for debugging or getting quick access to the files in a container.
$ docker run nginx
$ docker ps #note the container id
$ docker exec -it <container ID> /bin/bash
# echo "hello" > demo.txt
# ls
# exit
$ docker export <container ID> > "mynginx.tar"
$ tar -tvf mynginx.tar
$ docker import - mynginx < mynginx.tar #mynginx is new image name
$ docker images
$ docker run mynginx ls #shows demo.txt

Docker Build ARGS


Set variables during build time:
ARG some_variable_name
FROM mcr.microsoft.com/dotnet/sdk:5.0 as build-env
WORKDIR $some_variable_name
ENV env_var_name "default-value"
CMD ["printenv"]
docker build -t demoimage --build-arg some_variable_name=app .

Setting Environment Variables:


docker run demoimage printenv //env_var_name is "default-value"
docker run -e env_var_name=abcd demoimage printenv //env_var_name is "abcd"

Passing Environment Variables From the Host Into a Container:


docker run -e env_var_name demoimage printenv //env_var_name is value as set on host

MSSQL Server
Develop Intelligence – Docker Creating Custom Docker Images
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=Password_*123" --name "sql-demo" -e
"MSSQL_PID=Developer" -d -p 1412:1433 -v db-data-sqlsrv:/var/opt/mssql mcr.microsoft.com/mssql/server:2019-
latest

MySQL Server
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=Password_*123 -d -p 3306:3306 -v db-data-
mysql:/var/lib/mysql mysql:latest

You might also like