Docker Lesson 2 — Manipulating Containers with the Docker Client

Yao-Shang Tseng
Sep 2, 2018 · 2 min read

It’s the note for Docker and Kubernetes: The Complete Guide
https://www.udemy.com/docker-and-kubernetes-the-complete-guide/

Creating and Running a Container from an Image
# docker -> Reference the Docker Client
# run -> Try to create and run a container
# <image name> -> Name of image to use for this container
# command! -> Default command override
$ docker run <image name> command!EXAMPLE: $ docker run busybox echo hi there
$ docker run busybox ls
$ docker run -it bysybox sh
--
List all running containers
# docker -> Reference the Docker Client
# ps -> List all running containers
# -a -> --all, List all containers
$ docker ps -a--Create a Container
# docker -> Reference the Docker Client
# create -> Try to create the container
# <image name> -> Name of image to use for this container
$ docker create <image name>EXAMPLE: $ docker create hello-world
--
Start a Container
# docker -> Reference the Docker Client
# start -> Try to start the container
# <container id> -> ID of the container to start
# -a -> print all message from this container
$ docker start <container id>EXAMPLE: $ docker start -a 9281d2537dfjksldfshdjfkhsdkfdsjlkfjklsd
--
Remove unused container$ docker system prune
--
Get logs from a container
# docker -> Reference the Docker client
# logs -> Get logs
# <container id> -> ID of the container to look at
$ docker logs <container id>EXAMPLE: $ docker logs 9281d2537dfjksldfshdjfkhsdkfdsjlkfjklsd
--
Stop a Container$ docker stop <container id>EXAMPLE: $ docker stop 9281d2537dfjksldfshdjfkhsdkfdsjlkfjklsd
--
Kill a Container$ docker kill <container id>EXAMPLE: $ docker kill 9281d2537dfjksldfshdjfkhsdkfdsjlkfjklsd
--
Execute an additional command in a container
# docker -> Reference the Docker Client
# exec -> Run another command
# -it -> -i, -t, Allow us to provide input to the container
# <container id> -> ID of the container
# <command> -> Command to execute
$ docker exec -it <container id> <command>EXAMPLE: $ docker exec -it 9281d2537d redis-cli
$ docker exec -it 9281d2537d sh
--

docker run = docker create + docker start

we cannot start with command for an existing container

EXAMPLE(error) : docker start -a <container id> echo hi there
# (error) you cannot start and attach multiple container at once.

Command Processors
--------------------------------------------
| bash | powershell | zsh | sh |
--------------------------------------------
EXTRA
$ export b=5
$ echo $b
5

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade