Docker basic commands to survive

Created: Sept. 6, 2019, 1:28 p.m.
Update: March 31, 2020, 11:14 p.m.
Basic commands to create a docker image

Environment

Linux Ubuntu 18.04 LTS
Root (sudo) permission is required
docs
https://docs.docker.com/
https://www.digitalocean.com/community/tutorials/how-to-share-data-between-docker-containers
https://www.digitalocean.com/community/tutorials/how-to-share-data-between-the-docker-container-and-the-host
install
$ apt-get update
$ apt-get install docker.io

To create a container based in Ubuntu named docker-my-project, go to instance and use bash.

$ sudo docker run -ti --name docker-my-project --hostname dev ubuntu /bin/bash
use as real host, apt-get update, upgrade and others command.

To leave docker without stop proccess

to save all changes without lost data use combination keys crtl+p+q
$ sudo docker stop docker-my-project

To come back to docker

$ sudo docker attach docker-my-project 

Delete a docker

$ sudo docker rm docker-my-project

List all containers

$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
daf808f76463        ubuntu              "/bin/bash"         16 hours ago        Exited (130) 45 minutes ago                       docker-barragem
fff608813152        ubuntu              "/bin/bash"         6 weeks ago         Exited (130) 6 weeks ago                          docker-my-project

What containers is running?

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
28787e9f2b89        ubuntu              "/bin/bash"         5 days ago          Up 10 seconds                           docker-my-project

Start, stop and restart a container

$ sudo docker start docker-my-project

Copy file from localhost to container

$ sudo docker cp /tmp/django-1.4.22.tar.gz docker-my-project:/opt/django-1.4.22.tar.gz

To create a image from container

$ sudo docker commit docker-my-project docker-image-my-project

List images

$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE 
docker-image-my-project latest 8dcfeb565a6e 4 seconds ago 213MB

Delete a image

$ sudo docker rmi docker-my-project
Force delete
Error response from daemon: conflict: unable to remove repository reference "gpsi-1.4" (must force) - container 3bc390580b0b is using its referenced image e8e96a879a09
$ sudo docker rmi docker-my-project

Import and Export a image

export to a tar file
$ sudo docker save image-my-project > docker-image-my-project.tar
import
$ sudo docker load < docker-image-my-project-image.tar
$ sudo docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
<none>                 <none>              c342567970ba        11 minutes ago      1.3GB
python                 2.7                 55b47eef9c39        3 months ago        886MB
mariadb                latest              99c1098d5884        3 months ago        355MB
ubuntu                 latest              a2a15febcdf3        3 months ago        64.2MB

$ sudo docker tag c342567970ba my-project
$ sudo docker images

REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
my-project             latest              c342567970ba        14 minutes ago      1.3GB
python                 2.7                 55b47eef9c39        3 months ago        886MB
mariadb                latest              99c1098d5884        3 months ago        355MB
ubuntu                 latest              a2a15febcdf3        3 months ago        64.2MBrun image
$ sudo docker run --hostname dev -i -t my-project /bin/bash
docker ps running
$ sudo docker ps -a
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS                       PORTS               NAMES
71f5f017d03c        docker-barragem        "/bin/bash"              8 minutes ago       Up 8 minutes                                     upbeat_nightingale

How to run all theses line command automatitly?

$ sudo docker start docker-my-project
start mysql server
$ sudo docker exec -ti docker-my-project /etc/init.d/mysql start&
start django dev server
$ sudo docker exec -ti docker-my-project /root/dev/project/manage.py runserver 172.17.0.2:8000&
open at browser http://172.17.0.2:8000

Open a x-terminal / xfce4-terminal from container

Is boring to work in Docker when you need many terminals for line command, bash command and text editor. The best way is to open X terminal for open many tabs without exit a Docker instance.
run container from image and set variables to export display and share data
$ sudo docker run --name=ubuntu-xterminal -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -v $HOME:$HOME --hostname dev -i -t ubuntu
$ apt-get update
$ apt-get install xfce4-terminal
$ crtl + p + q  (quit container)
--name=ubuntu-xterminal (container name)
ubuntu (image name)
-v volume export
-e variable export
localhost, run xterminal from container
$ xhost + $ sudo docker exec -ti -e DISPLAY=$DISPLAY ubuntu-xterminal xfce4-terminal

Remove all unused volumes

To remove all unused volumes use the docker image prune command:
$ sudo docker volume prune
You’ll be prompted to continue, use the -f or --force flag to bypass the prompt.

Inspect gestorpsi_docker

$ sudo docker inspect gestorpsi_docker
"CreatedAt": "2019-09-11T16:13:43-03:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/gestorpsi_docker/_data",
"Name": "gestorpsi_docker",
"Options": null,
"Scope": "local"



        

Tiago de Souza Moraes - teago.futuria.com.br - 2020 // CSS by UIKIT CSS