I want to do this! Reverse lookup docker command when

GRED Web Tampering Check Cloud

Hello.
I'm Mandai, in charge of Wild on the development team.

I often forget docker commands, so I wrote this in a fit of anger.
Now I won't forget them again.


 

Get a list of Docker images

docker images

 

Get a list of Docker containers

# Only currently running Docker containers docker ps # All Docker containers including stopped Docker containers docker ps -a # Display only Docker container IDs docker ps -q

 
"docker ps -q" has no use on its own, but it is useful when deleting docker containers in bulk

It may be difficult to imagine at first what the difference is between a docker image and a docker container, but it's like the relationship between a physical CD and an ISO file (which is hard to understand)

 

Name and start a docker container

docker run --name [container name]

 
If you name a Docker container, you must remove it with the "docker rm" command before you can start a Docker container with the same name

 

Share files between docker container and host

docker run -v [host directory path]:[docker container directory path]

 

Port forwarding to make the Docker container port available on the host

docker run -p [host port number]:[docker container port number]

 

Connecting Docker containers

docker run -link [docker container name or ID][:[alias]]

 
Aliases are useful when you don't have a name for your docker container or you have a super long docker container name

The mechanism is simple, using hosts. If you compare the hosts files, you will see the following:

# Start the MySQL container to connect to $ docker run -d --name test -e MYSQL_ROOT_PASSWORD=root mysql:latest 529c5facaed9694dbcb36e9e3d3eae0350e26e182f70c72b06d2914dcf8c0222 # Start the Docker container with the name and alias specified in the link destination $ docker run -it --link test:db php:latest cat /etc/hosts 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.17.0.3 db 529c5facaed9 test 172.17.0.4 ffae57a81bc1 # Start a Docker container by specifying only the name as the link destination $ docker run -it --link test php:latest cat /etc/hosts 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.17.0.3 test 529c5facaed9 172.17.0.4 128c598dea8a

 
The difference is whether the alias is listed in the hosts file or not

 

Start a docker container in detached mode

docker run -d [docker image name]


 Detached mode is a state where the container does not enter the container and runs in the background.
I thought it was a daemon.

 

Run a Docker container in the foreground and run any command

docker run [docker image name] [command] 

 

Start a docker container in the foreground and attach to the docker container

docker run -it [docker image name] [any shell such as /bin/bash]


 The -i option attaches STDIN (standard input). In other words, input from the console will flow into the specified Docker container.
If you use "docker run -i hogehoge /bin/bash", you can type console commands, but they will not be reflected on the screen.

The -t option assigns a TTY to the Docker container. Without this, you will not be able to see the console in the Docker container even if you attach.
If you run "docker run -t hogehoge /bin/bash", you will see the prompt in the Docker container, but typing will not be reflected on the screen.

So, to work inside a Docker container, you need both of the above options

 

Set environment variables in a Docker container on startup

docker run -e [variable name=value]

 
This will allow you to specify the root password for mysql

docker run -e MYSQL_ROOT_PASSWORD=root

 

Attach to a running Docker container

docker attach [docker container ID]

 
To detach from a Docker container, use the shortcut keys CTRL + P, Q to return to the host

 

I want to run a shell command in a Docker container

docker exec [docker container ID] [/bin/bash or other shell to launch]

 

I want to stop a docker container

docker stop [docker container ID]

 

I want to delete a docker container

docker rm [docker container ID]

 

I want to delete all the Docker containers

docker rm $(docker ps -aq)

 
This is a way to get the Docker container ID in a subshell. The -q option of docker ps is the best use case

 

I want to delete a docker image

docker rmi [docker image name]

 

I want to know information about Docker containers

docker inspect [docker container ID]

 

If you want to get the IP address used by a Docker container (this comes with some extra information, but it serves the purpose)

docker inspect [docker container ID] | grep IPAddress

 

 
That's it.

If you found this article helpful , please give it a like!
2
Loading...
2 votes, average: 1.00 / 12
7,966
X facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Yoichi Bandai

My main job is developing web APIs for social games, but I'm also fortunate to be able to do a lot of other work, including marketing.
Furthermore, my portrait rights in Beyond are treated as CC0 by him.