Basic docker commands
17 Oct 2019
Cool Docker course: Docker and Kubernetes: The Complete Guide
Docker Container is a process or a set of processes that have a group of resources assigned to it.
Docker image is a File System Snapshot + Startup Command
docker run <image-name> –> create and start container
ex : docker run busybox , docker run hello-world
———
docker run <image name> <command> –> create and start container with specific command,
ex: docker run busybox echo hi there , docker run busybox ls
———
docker run -id <image-name> sh –> create and start container and open its command shell
———
docker ps –> show running docker images
docker container ls –> same (show running docker images)
docker ps --all –> show history for all docker images execution
———
Container lifecycle:
docker run=docker create+docker start -a
1.1docker create <image-name>= Creates a writeable container layer over the specified image and prepares it for running the specified command
1.2docker start <container-id> <...contaider-id>= Start one or more stopped containers- Exit naturally or
docker stop(send SIGTERM message) ordocker kill(send SIGKILL message) - Remove container
docker rm docker system prune- remove all docker garbage from the system
———
dockder start -a <containe-id> –> start docker container and -a(attach) watch for container output and print it out to terminal
———
docker system prune –> remove all stopped containers, dangling images, unused networks, build cache
———
docker logs <container-id> –> show container logs
———
docker stop <container-id> –> stop container by id (container will be killed if it was not stopped after 10 secs)
docker container stop <container-name> –> stop container by name
docker kill <container-id> –> kill container
———
docker build . –> build a docker image from Dockerfile in cur dir
docker build -t <image-name> . –> build a docker image and tag it
usually tag looks like [your-docker-id]/[your-project-name]:[version] (by default version is latest)
docker build -f Dockerfile.dev -t sergpank/frontend . –> build image from specific file (Dockerfile.dev) and tag it
———
docker container rm <container-name> –> remove container from history by name
docker image ls –> list all installed docker containers
docker images –> same as above ^
docker image rm <container-name> –> remove installed container
———
docker stop $(docker ps -a -q) –> stop all running docker containers
docker rm $(docker ps -a -q) –> remove all running containers
———
docker network create docker_default –> create network with name docker_default
———
docker exec -it <container-id> <command>
ex: docker exec -i <98c54f967c4e> redis-cli –> start redis cli inside running redis container (“-it” - type input direct into container)
- -i –> interactive, attach standard-in stream
- -t –> allocate pseudo tty (format output text)
docker exec -it <container-id> sh –> open command shell in docker container
———
Container port mapping
docker run -p <port-for-incoming-requests>:<port-inside-container> <image-name>
———
Dockerfile example
# file name = Dockerfile
# base image
FROM node:alpine
# install dependencies
WORKDIR /demo
COPY package.json ./
RUN npm install
# if I will update index.js, then only that step will be executed on containder rebuild
# and all previous steps will rein cached
# and that will minimize cache busting and rebuilds
COPY index.js ./
# default command
CMD ["npm", "start"]
———
docker-compose
# docker-compose.yml file example
version: '3'
services:
redis-server:
image: 'redis'
node-app:
build: . #assumes that cur dir contains Dockerfile
ports:
- "4001:8081"
### more complex example
networks:
counter-net:
volumes:
counter-vol:
services:
web-fe:
build: .
command: python app.py
ports:
- target: 8080
published: 5001
networks:
- counter-net
volumes:
- type: volume
source: counter-vol
target: /app
redis:
image: "redis:alpine"
networks:
- counter-net
docker compose up —> start docker-compose script
docker compose down —> stop all started containers with docker-compose
docker compose down -v —> stop all started containers and remove volumes
docker compose ps —> show all docker containers started with docker-compose
———
Volumes are managed totally independent from containers
# main command
docker volume
# list volumes
docker volume ls
# create volume
docker volume create --name myvol
# inspect volume
docker volume inspect volume
# location of volumes (this works only on Linux)
sudo ls -l /var/lib/docker/volumes
# delete volume
docker volume rm myvol
# create volume and mount it at /vol to container
docker run -dit alpine \
--name voltest \
--mount source=ubervol,target=/vol \
Bookmarking a volume:
docker run -it -p 3000:3000 -v /app/node_modules -v $(pwd):/app sergpank/frontend
where
-it –> run interacctively (otherwise node-js does not start in this example)
-p 3000:3000 –> port mapping
-v /app/node_modules –> do not override [/app/node_modules] dir in container
-v $(pwd):/app –> map [present-work-dir] into [/app] dir in container
sergpank/frontend –> image name
docker-compose.yml for such case will look like:
version: '3'
services:
frontend:
build: .
ports:
- "3000:3000"
volumes:
- /app/node_modules
- .:/app
———
Example
docker run --detach --publish=80:80 --name=webserver nginx
- start nginx container in
detachedmode - map it to port 80
- assign a
nameto it –webserver. - Now we can type
localhostin browser and see that NGINX is running.
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
602f3d74acf3 nginx "nginx -g 'daemon of…" 31 seconds ago Up 30 seconds 0.0.0.0:80->80/tcp webserver
———
Formatted output for ps
$ docker ps --format 'table {{.ID}}\t{{.Names}}\t{{.Ports}}\t{{.Status}}'
$ docker ps --format "{{.ID}}: {{.Command}}"
Placeholder | Description
--------------|------------
.ID | Container ID
.Image | Image ID
.Command | Quoted command
.CreatedAt | Time when the container was created.
.RunningFor | Elapsed time since the container was started.
.Ports | Exposed ports.
.Status | Container status.
.Size | Container disk size.
.Names | Container names.
.Labels | All labels assigned to the container.
.Label | Value of a specific label for this container. For example '{{.Label "com.docker.swarm.cpu"}}'
.Mounts | Names of the volumes mounted in this container.
.Networks | Names of the networks attached to this container.
# if it is necessary to get some additional info:
$ docker ps -q | xargs docker inspect --format '{{ .Id }} - {{ .Name }} - {{ .NetworkSettings.IPAddress }}'
8624b7c8eb46f2eea0aa371574fd1d5aec5f941a19184a57d497f29230d50e7f - /sleepy_chaplygin - 172.18.0.2