Containers

See containers

  • docker ps → See containers running

  • docker container ls → See containers running

  • docker ps -a → See all containers

  • docker container ls -a → See all containers


Run containers

  • docker run docker/whalesay cowsay Docker → Run cowsay with message Docker

  • docker run node → Run node

  • docker run ubuntu → Run ubuntu

  • docker run nginx → Run nginx

-it

  • docker run -it node → Run node and use node console

  • docker run -it ubuntu → Run ubuntu and use node console

-d

  • docker run -d nginx → Run nginx background

-p

  • docker run -d -p 80:80 nginx → Run nginx background and in browser port 80

  • docker run -d -p 3000:80 nginx → Run nginx background and in browser port 3000

--name

  • docker run -d -p 80:80 --name nginx-container nginx → Run nginx with name nginx-container

--mysql with network

  • docker run -d -p 3306:3306 --name mysql_container --rm --network mysqlnetwork -e MYSQL_ALLOW_EMPTY_PASSWORD=True mysqlimage → Run mysql image with network

--mysql image

  • docker run -d -v /var/lib/mysql -p 3306:3306 --name mysql_container --rm -e MYSQL_ROOT_PASSWORD=docker -e MYSQL_DATABASE=nodeDocker -e MYSQL_USER=felipe -e MYSQL_PASSWORD=dockerfelipe mysql:5.7

Stop container

  • docker stop id → Stop docker container by container id

  • docker stop name → Stop docker container by container name


Start container

  • docker start id → Start docker container by container id

  • docker start name → Start docker container by container name

  • docker start -i name → Start docker and see logs


Logs container

  • docker logs id → Logs docker container by container id

  • docker logs name → Logs docker container by container name


Remove container

  • docker rm id → Remove docker container by container id

  • docker rm name → Remove docker container by container id

-f

  • docker rm -f id → Force stop and remove docker container by container id

  • docker rm -f name → Force stop and remove docker container by container id


Copy container file or folder to other folder

  • docker cp node_container:/app/app.js ./copy/ → Copy the file app.js to a local folder copy

Get informations docker container

  • docker top container → See informations about container

  • docker inspect container → See id, createdData, image, etc

  • docker stats → See how many memory, cpu and some informations about every containers



Images

Dockerfile

  • FROM → Base image

  • WORKDIR → Application directory

  • EXPOSE → Application port

  • COPY → Files that need to be copied


Example:

FROM node

WORKDIR /app

COPY package*.json .

RUN npm install

COPY . .

EXPOSE 3000

CMD ["node", "app.js"]

  • docker build . → Build Dockerfile "docker build (root of Dockerfile)"

  • docker run -d -p 3000:3000 --name my_node id → Run Dockerfile

  • docker run -d -p 3000:3000 --name my_node name → Run Dockerfile

  • docker run -d -p 3000:3000 --name my_node --rm name → Run Dockerfile and when stop it is removed



See images

  • docker image ls → See images

  • docker images → See images


Dowloading image

  • docker pull python → Dowload python

Name for image

  • docker tag id name:tag → Change name and tag image

  • docker build -t name:tag . → Build with name and tag


Remove image

  • docker rmi name → Remove image by name

  • docker rmi id → Remove image by id

  • docker systmem prune → Remove all containers, images and networks that are't being used



Docker commands

  • docker login → Login to docker

  • docker logout → Logout docker

-docker build -t 17102003/first_image_node . → Get image to docker hub

  • docker push 17102003/first_image_node → Push image to docker hub

  • docker pull 17102003/first_image_node → Update local image with hub image

  • docker build -t 17102003/first_image_node:1.0 . → Build image with a tag

  • docker push 17102003/first_image_node:1.0 → Push image with a tag

  • docker pull 17102003/first_image_node:1.0 → Pull image with a tag

  • docker run -d -p 3000:3000 --name test_image 17102003/first_image_node:1.0 → Run image



Volumes

See volumes

  • docker volume ls → See all docker volumes

Run with volume

Without name

  • docker run -d -p 80:80 --name node_container -v /data node_image → Run container and create a volume without name

With name

  • docker run -d -p 80:80 --name node_container -v name_volume:/var/www/html/messages node_image → Run container and create a volume with name

In local

  • docker run -d -p 80:80 --name php_container -v /mnt/c/Users/felip/workspace/2-study/udemy/docker/2-Volumes/messages:/var/www/html/messages --rm php_messages → Run container and create a volume and upload local folder

Read Only

  • docker run -d -p 80:80 --name php_container -v volumeleitura:/var/www/html:ro --rm php_messages → Create a read only volume

Create Volumes

  • docker volume create name → Create a volume

Inspect volume

  • docker volume inspect name → Inspect a volume

Remove Volume

  • docker volume rm name → Remove a volume

Remove unused volumes

  • docker volume prune → Remove unused volumes


Networks


Types

  • bridge → Connect container to other container

  • host → Connect container to Host

  • macvlan → Connect container to container with MAC address

  • nonde → Container can't connect to other

  • plugins → Connect container with custom network (other person)


See all networks

  • docker network ls → See all networks

Create network

  • docker network create my_network → Create a network

Another driver

  • docker network create -d macvlan my_network → Create a network with specific

Remove network

  • docker network rm my_network → Remove a network

Remove unused networks

  • docker network prune → Remove unused networks

Connect container a network

  • docker network connect network container → Connect container a network

Disconnect container a network

  • docker network disconnect network container → Disconnect container a network

Inspect network

  • docker network inspect networkname → Inspect a network


Compose


Run docker-compose

  • docker-compose up → Run docker-compose

-d

  • docker-compose up -d → Run docker-compose without console

Stop docker-compose

  • docker-compose down → Stop docker-compose