On the road again

In this article I will post some useful Docker commands

List containers:
$docker container ls
$docker ps (only running containers)
$docker ps -a (all containers)
Connect to container:
$docker attach c3bbaca72bed ###OR better:
$docker exec -it c3bbaca72bed /bin/bash (actually it runs an interactive command)
Create container and connect to it:
$docker run --name arababa -i -t ubuntu /bin/bash
Remove container:
$docker rm ID_or_name
$docker rm -f `sudo docker ps -a -q` (remove all containers)
 
Start container:
$docker start arababa (container will start with the same options we’d specified when we launched it with the docker run)
Create daemonized container:
$docker run --name arababa_daemon -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done" (will run in endless loop until stopped)
Container logs:
$docker logs arababa
$docker logs -f arababa (like tail -f)
$docker logs -ft arababa (like tail -f and with timestamps)

Download image from registry:

docker pull docker-dev-virtual.docker.mirantis.net/mirantis/oscore/oh/barbican:stein-bionic-2019

If it is impossible to connect to container and it is needed to view its file system you may export container to a tar file:

1. Run container (actually will fail since there is no any shell inside):
docker run --name arababa -i -t d838e656a0d2 /bin/bash
2. Export to tar:
docker export $(docker ps -lq) > requirements.tar

Information about container:

$docker inspect arababa
$docker inspect --format '{{ .NetworkSettings.IPAddress }}' arababa
List images:
$docker image ls
Local images live on our local Docker host in the /var/lib/docker directory. Each image will be inside a directory named for your storage driver, for example, aufs images live inside repositories, and repositories live on registries. The default registry is the public registry managed by Docker, Inc., Docker Hub.
 
Login to registry:
$docker login (credentials will be stored in the $HOME/.dockercfg file. SinceDocker 1.7.0 this is now $HOME/.docker/config.json)
Create own image with commit (this method is not recommended):
$docker commit -m "Custom image with Apache2" -a "MAINTAINER" d58a608a0589 arababa/apache2:webserver1 (create image from modified container)
$docker push arababa/apache2 (push to docker hub - need to login before, image should have a tag)​

Create own image with Dockerfile:
 
Dockerfile contents (./Dockerfile):
# Version: 0.0.1
FROM ubuntu:latest
LABEL maintainer="Ця електронна адреса захищена від спам-ботів. Вам необхідно увімкнути JavaScript, щоб побачити її."
RUN apt-get update; apt-get install -y nginx
RUN echo 'Hi, I am in your container' \
>/var/www/html/index.html
EXPOSE 80
$docker build -t="grudev/nginx:webserver1" .

Run container specifying port and detach (to run in foreground, -d):

$docker run -it -d -p 80 arababa/nginx:webserver1 /bin/bash
$docker run -itd -p 80 arababa/nginx:webserver1 nginx -g "daemon off;" (running nginx in non-daemonized mode, since if you run /usr/sbin/nginx the initial process will spawn children and will exit hence container will exit)
$docker run -itd -p 8080:80 arababa/nginx:webserver1 nginx -g "daemon off;" (map container`s port 80 to local port 8080, otherwise random port will be used)​

Copy files to/from a container:

$docker cp webapp:/opt/webapp/app.py .
$docker cp app.py webapp:/opt/webapp/
$docker restart webapp
 
Find image SHA commit:
docker inspect --format='{{index .RepoDigests 0}}' $IMAGE_ID
NETWORKING
 
Change default network for docker0 bridge:
cat /etc/docker/daemon.json:
{
  "bip": "192.168.1.5/24",
  "fixed-cidr": "192.168.1.5/25"
}
View current docker networks:
$docker network ls
Inspect certain network:
$docker inspect network bridge
Create new network with new subnet IP:
$docker network create my-network -d bridge --subnet 172.19.0.0/16
Run container on specified network:
$docker run -d --network=my-network --name db training/postgres
Connect existing container to a new network:
$docker network connect my-network webapp
 
VOLUMES
 
Create local folder and map it as a volume inside container:
$mkdir db
$docker run -d --name db -v /home/stack/db:/db training/postgres
Provide volumes from one container to other:
$docker create -v /dbdata -v /appconfig --name dbstore training/postgres /bin/true
$docker run -d --volumes-from dbstore --name db1 training/postgres
$docker run -d --volumes-from dbstore --name db2 training/postgres
Link containers:
$docker run -d -P --name webapp --link db:db training/webapp python app.py


Add comment