To continue the series of articles about Docker, I’d like to share about some basic Docker commands that we often used when we work with Docker.
0. Very first Docker command
The very first Docker command should be itself which shows how to to use the Docker commands, how many options, parameters, some basic Docker commands, etc.
1 |
$ docker |
For ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
$ docker Usage: docker [OPTIONS] COMMAND [arg...] docker daemon [ --help | ... ] docker [ --help | -v | --version ] A self-sufficient runtime for containers. Options: --config=~/.docker Location of client config files -D, --debug Enable debug mode -H, --host=[] Daemon socket(s) to connect to -h, --help Print usage -l, --log-level=info Set the logging level --tls Use TLS; implied by --tlsverify --tlscacert=~/.docker/ca.pem Trust certs signed only by this CA --tlscert=~/.docker/cert.pem Path to TLS certificate file --tlskey=~/.docker/key.pem Path to TLS key file --tlsverify Use TLS and verify the remote -v, --version Print version information and quit Commands: attach Attach to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container |
The next essential one which gives us summarized information about how many Docker containers in our environment, how many is Running, Paused, Stopped. How many Docker images in our environment, the server version, Docker Root Dir, Total Memory, Storage Driver and so on.
1 |
$ docker info |
For ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
$ docker info Containers: 2 Running: 0 Paused: 0 Stopped: 2 Images: 1 Server Version: 1.11.0 Storage Driver: aufs Root Dir: /var/lib/docker/aufs Backing Filesystem: extfs Dirs: 6 Dirperm1 Supported: false Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge null host Kernel Version: 3.13.0-85-generic Operating System: Ubuntu 14.04.4 LTS OSType: linux Architecture: x86_64 ... |
To check which version of Docker is being installed in your operating system, included Docker client and Server, you can use below command:
1 |
$ docker version |
For ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$ docker version Client: Version: 1.11.0 API version: 1.23 Go version: go1.5.4 Git commit: 4dc5990 Built: Wed Apr 13 18:34:23 2016 OS/Arch: linux/amd64 Server: Version: 1.11.0 API version: 1.23 Go version: go1.5.4 Git commit: 4dc5990 Built: Wed Apr 13 18:34:23 2016 OS/Arch: linux/amd64 |
1. Some Docker Registry, Repository Commands
1.1. Search For a Term on the Docker Registry
This Docker command used when you want to search for any image you want. For example, you want to search for ubuntu docker, centos docker, chef docker,..
1 |
$ docker search [OPTIONS] TERM |
For ex: Below example will search for Docker Apache Kafka on the registry. You can see that the first one has 143 rated stars. I’m going to pull it on the next command.
1 2 3 4 5 6 7 |
$ docker search kafka NAME DESCRIPTION STARS OFFICIAL AUTOMATED wurstmeister/kafka Multi-Broker kafka 0.8.x image 143 [OK] spotify/kafka A simple docker image with both Kafka and ... 108 [OK] sheepkiller/kafka-manager kafka-manager 31 [OK] ches/kafka Apache Kafka. Tagged versions. JMX. Cluste... 30 [OK] digitalwonderland/kafka Latest Kafka - clusterable 7 [OK] |
1.2. Pull a Docker Image from Registry to Local Machine
1 |
$ docker pull [OPTIONS] NAME[:TAG|@DIGEST] |
For ex: Below command will pull the wurstmeiter/kafka image to local machine.
1 2 3 4 5 6 7 8 |
$ docker pull wurstmeister/kafka Using default tag: latest latest: Pulling from wurstmeister/kafka ee54741ab35b: Downloading [=> ] 58.29 kB/2.125 MB a3ed95caeb02: Download complete 9c9e170ca59d: Pulling fs layer 9515f029f814: Retrying in 1 second |
2. Some Docker commands used for Docker images.
This section list out some Docker commands to manipulate Docker images.
2.1. List all Docker Images in Our Environment
1 |
$ docker images |
1 2 3 |
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest 690ed74de00f 6 months ago 960 B |
2.2. Remove a Docker Image
1 |
$ docker rmi image_name |
1 |
$ docker rmi hello-world |
1 2 |
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container c09660ed4522 is using its referenced image 690ed74de00f |
1 2 3 |
$ docker rmi -f hello-world Untagged: hello-world:latest Deleted: sha256:690ed74de00f99a7d00a98a5ad855ac4febd66412be132438f9b8dbd300a937d |
2.3. Remove all Docker Images
There will be some cases, you want to remove all Docker images instead of one by one. Let’s use the below command:
1 |
$ docker rmi $(docker images -q) |
2.4. To Show History of a Docker Image
1 |
$ docker history image_name |
For ex:
1 2 3 4 |
$ docker history hello-world IMAGE CREATED CREATED BY SIZE 690ed74de00f 6 months ago /bin/sh -c #(nop) CMD ["/hello"] 0 B <missing> 6 months ago /bin/sh -c #(nop) COPY file:1ad52e3eaf4327c8f 960 B |
3. Some Docker Commands Used for Container
Below are some Docker commands which can be used for Docker containers.
3.1. Create a Container from a Docker Image
1 |
$ docker create image_name |
1 2 |
$ docker create hello-world 2470fa9d4b7edf6318ede1d90a6a263802177231439abd025053cf58d3223b3b |
3.2. Start a Docker Container
1 |
docker start container_id |
1 2 3 4 |
$ docker start -i 2470fa9d4b7edf6318ede1d90a6a263802177231439abd025053cf58d3223b3b Hello from Docker. This message shows that your installation appears to be working correctly. |
3.3. Start and Run a Container
1 |
$ docker run image_name |
1 2 3 4 |
$ docker run -i hello-world Hello from Docker. This message shows that your installation appears to be working correctly. |
3.4. See All The Docker Containers
1 2 3 |
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS 2470fa9d4b7e hello-world "/hello" 6 minutes ago Exited (0) 3 minutes ago |
1 2 |
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS |
3.5. Inspect a Docker Container
1 |
$ docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
$ docker inspect 78ae776f45c6 [ { "Id": "78ae776f45c6207504c33501a4b17c771c0c441de2a20603871fc660dff71d41", "Created": "2016-04-26T17:24:54.618594804Z", "Path": "/hello", "Args": [], "State": { "Status": "exited", "Running": false, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 0, "ExitCode": 0, "Error": "", "StartedAt": "2016-04-26T17:24:54.794157192Z", "FinishedAt": "2016-04-26T17:24:54.799527453Z" }, ... ] |
3.6. Stop a Docker Container
1 |
$ docker stop [OPTIONS] CONTAINER [CONTAINER...] |
1 |
docker stop 2470fa9d4b7e |
3.7. Kill a Docker Container
1 |
$ docker kill [OPTIONS] CONTAINER [CONTAINER...] |
1 |
$ docker kill 2470fa9d4b7e |
3.8. Remove a Docker Container
1 |
docker rm <CONTAINER ID> |
1 |
docker rm 2470fa9d4b7e |
3.9. Remove All Docker Containers
1 |
docker rm $(docker ps -a -q) |
3.10. Get Bash Shell Inside a Docker Container
1 |
docker exec -i -t "container_id" bash |
1 |
$ docker exec -i -t 2470fa9d4b7e bash |
https://docs.docker.com/engine/reference/commandline/
4. Conclusion
We have learned some basic Docker commands which are often used when we manipulate with Docker. Hopefully, this article bring you a useful reference. Below are other articles related to Docker. If you’re interested in, you can refer to the following links:
How to Get IP Address of a Docker Container
Copy Files, Folders from Host to Docker Container and Vice Versa
Install Docker on Ubuntu 16.04, 15.10, 14.04 Step By Step
Introduction to Docker Compose
Vagrant Docker Provider Tutorial