Docker study notes

Container related commands

Run – start a container

 docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

The default tag of the image is latest.

Docker would search for the image in the host, otherwise would download from Docker hub if there’s no local copy.

Container only lives until the process in it lives. For example, if we run ‘docker run ubuntu’, container will be destroyed right after it’s up.

Keep STDIN open even if not attached(-i/ –interactive)

docker run -i myimage

This is interactive mode that container will take input from the keyboard.

Assign name and allocate pseudo-TTY (–name, -it)

docker run --name test -it debian

This example runs a container named test using the debian:latest image. The -it instructs Docker to allocate a pseudo-TTY connected to the container’s stdin; creating an interactive bash shell in the container. In simpler words, -it prints prompt and gets input.

Publish or expose port (-p, –expose)

docker run -p 127.0.0.1:80:8080/tcp ubuntu bash

This binds port 8080 of the container to TCP port 80 on 127.0.0.1 of the host machine. The host port can only be mapped once.

Use environment variables (–env/-e)

docker run -e "today=Sunday" -e "season=Winter" alpine

It’s in the form of key-value pair. Docker supports multiple environment variables set at runtime.

Mount a volume (–volume/-v)

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

The -v flag mounts the current working directory into the container. –mount is another expression way, which is more explicit and verbose.

docker run --mount source=opt/data,target=var/lib/mysql mysql

https://docs.docker.com/storage/volumes/ explains more about mount and volume.

PS – list containers

docker ps

This would display running containers. Adding ‘ -a‘ to the end of the above command would show inactive containers as well.

Inspect – provides detailed information

docker inspect [OPTIONS] NAME|ID [NAME|ID...]

Details include environment information.

logs

docker logs [OPTIONS] NAME

It batch-retrieves logs present at the time of execution.

Stop – stop a container

docker stop container_name
docker stop container_id

Container name is a random name given to the container which helps us to refer to.

Rm – remove a container

docker rm container_name
docker rm container_id

Since container name and id are unique, they can be used as identifiers for removal.

Image related commands

Images – list images

docker images

Rmi – remove images

docker rmi image_name

One thing worth mentioning is that all dependent containers need to be deleted before deleting images.

Pull – download image

docker pull image_name

As we have covered earlier, when starting a container, images will be pulled down first.

Build – build an image

docker build [OPTIONS] PATH | URL | -

–file/-f : the name of Dockerfile. The default name is Dockerfile. If so, no need to provide this option. The full command is like ‘docker build .’.

–tag/-t: Name and optionally a tag in the ‘name:tag’ format. Tag is latest by default.

If the image is built from remote URL such as git, branch or commit is separated by ‘#’ in build syntax, such as ‘myrepo.git#mytag’. If it sits under a specific folder, use format as ‘myrepo.git#master:myfolder’.

Tricks

  • How can we see the base image without Dockerfile?

docker run python:3.6 cat /etc/*release*

  • How to remove unused images at once?

docker image prune -a

More discussion about dangling image and unused image at http://stackoverflow.com/questions/45142528/ddg#45143234

Leave a comment