Tuesday, July 18, 2017

Docker - Basic commands

Run Docker as a service:  sudo systemctl start docker

docker build -t friendlyname .  # Create image using this directory's Dockerfile, notice '.' for current directory!
docker run -p 4000:80 friendlyname  # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyname         # Same thing, but in detached mode
docker ps                                 # See a list of all running containers
docker stop <hash>                     # Gracefully stop the specified container
docker ps -a           # See a list of all containers, even the ones not running
docker kill <hash>                   # Force shutdown of the specified container
docker rm <hash>              # Remove the specified container from this machine
docker rm $(docker ps -a -q)           # Remove all containers from this machine
docker images -a                               # Show all images on this machine
docker rmi <imagename>            # Remove the specified image from this machine
docker rmi $(docker images -q)             # Remove all images from this machine
docker login             # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag  # Tag <image> for upload to registry
docker push username/repository:tag            # Upload tagged image to registry
docker run username/repository:tag                   # Run image from a registry

Stop all running containers.

docker stop$(docker ps -q)

Remove all stopped containers.

docker rm $(docker ps -a -q)
This will remove all stopped containers by getting a list of all containers with docker ps -a -q and passing their ids to docker rm. This should not remove any running containers, and it will tell you it can’t remove a running image.

Remove all untagged images

In the process of running docker I had accumulated several images that are not tagged. To remove these I use this command:
docker rmi $(docker images -a | grep "^<none>" | awk '{print $3}')
This works by using rmi with a list of image ids. To get the image ids we call docker images then pipe it to grep "^<none>". The grep will filter it down to only lines with the value “<none>” in the repository column. Then to extract the id out of the third column we pipe it to awk "{print $3}" which will print the third column of each line passed to it.
Great blog here to manage Docker Images on local disk.

Clean up Volumes

Reclaim memory that could cause an issue on MAC/OS.

docker system prune --volumes

Build the app

$ ls
Dockerfile  app.py   requirements.txt

docker build -t friendlyhello .
Where is your built image? It’s in your machine’s local Docker image registry:
$ docker images

REPOSITORY            TAG                 IMAGE ID
friendlyhello         latest              326387cea398

Run the app

Now let’s run the app in the background, in detached mode:
docker run -d -p 4000:80 friendlyhello
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED
1fa4ab2cf395        friendlyhello       "python app.py"     28 seconds ago

Now use docker stop to end the process, using the CONTAINER ID, like so:
docker stop 1fa4ab2cf395

Log in with your Docker ID

If you don’t have a Docker account, sign up for one at cloud.docker.com. Make note of your username.
Log in to the Docker public registry on your local machine.
docker login

Tag the image

docker tag friendlyhello john/get-started:part1

Publish the image

Upload your tagged image to the repository:
docker push username/repository:tag

Pull and run the image from the remote repository

From now on, you can use docker run and run your app on any machine with this command:
docker run -p 4000:80 username/repository:tag

Access running container

Find container ID: $ docker ps -a
sudo docker exec -ti <CONTAINER_ID> bash


Run single service from Docker-compose

docker-compose up -d mongodb
docker-compose stop mongodb
           docker-compose down -v     will remove all the existing volumes for the services used in the compose.


After creating your YAML file as above, use docker-compose config to check the validity of your compose file for any errors. If there are no errors, then you are ready to bring up the containerized services.

Check docker-compose.yml file for errors

To start up your services; run docker-compose up -d --build if it is your first time running these services.
If you made a change or you want to destroy and recreate the containers again, run: docker-compose up -d --build --force-recreate -t 0

To bring stop and remove all the containers use: docker-compose down

 Check log for that specific service

          docker-compose logs -f mongodb


Use builkit to improve building time:

COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker-compose  up -d --build --force-recreate --no-deps izoaGateway



Install ping in a container running Redhat RHEL.

As root:

docker exec -u 0 -it <container_id> /bin/bash

microdnf update 

microdnf install iputils


No comments:

Post a Comment