Docker Introduction & Commands

  Docker

Docker run >

Links

Download Docker:

http://docs.docker.com (Make sure you open the page wide enough to see the side bar.)

https://docs.docker.com/install/linux/docker-ce/ubuntu/

Download pre-built images:

http://hub.docker.com

Introduction

5. Setup and Install Docker

https://www.udemy.com/course/learn-docker/learn/lecture/7928348#overview

Download: https://docs.docker.com/ > Get Docker

Ubuntu: https://docs.docker.com/engine/install/ubuntu/

sudo apt update
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

# Add Docker’s official GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# Add stable repository
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

# Install Docker
sudo apt update 
sudo apt install docker-ce docker-ce-cli containerd.io

Using Docker Hub

https://hub.docker.com

Create your first container – Whalesay

sudo docker run docker/whalesay cowsay Hello World!

 

 

Docker Commands

https://www.udemy.com/learn-docker/learn/lecture/7894010#overview

Docker official reference link: https://docs.docker.com/engine/reference/commandline/docker/

attach

Attach to a previously detached container

docker attach <container_id | container_name>
  • When using the container_id, you only need to use the first few characters as long as they are unique to that container!

build

Builds an image based on the contents of `Dockerfile`

  • -t Tag: Name the new image
  • -f file: Defines a file to build from other than the default ‘Dockerfile’
docker build . -f mybuild -t myapp:latest

exec

Runs a command in the container

docker exec <container_id | container_name> <command>

Examples:

Log into a running container. Type ‘exit’ to exit out.

docker exec -it <container_name> /bin/bash

Display the contents of the /etc/hosts file

docker exec <container_name> cat /etc/hosts

history

Lists the steps required to build an image.

Discussed in Section 18

docker history <ImageName | ImageId>
  • Also shows the size of each step

images

Lists all images available on the system

docker images

inspect

Prints the information about the container in a JSON format

This is actually discussed in the ‘Run’ lecture, but is not a run command.

docker inspect <ContainerName | ContainerId>
  • Great for finding all configuration information about a container
    • IP address and port
    • passwords
    • status

logs

Display the logs from a Container.

docker logs <ContainerId | ContainerName>

 

ps

Lists all running containers

docker ps

Options

-a
Include all running and previously stopped containers.

pull

Downloads a pre-created public image from the docker repository

docker pull <image_name>
  • Only downloads the image.  Does not start it as it would if you were pulling using the ‘run’ command.

rm

Removes a container and reclaims the diskspace

docker remove <container_id | container_name>
  • Good for cleaning up history.
  • list multiple container ids to remove several with one command
    • docker rm 123 654 852

Options

-f –force
Forces removal of a running container

-v –volumes
Removes the volumes associated with a container

-f $(docker ps)
Removes all running containers

-f ($docker ps -a)
Removes ALL containers, stopped and running.

$(docker ps -a -q)
Removes all stopped containers.  Has no affect on running containers (an error is generated instead)

docker rm $(docker -a -q)

rmi

Removes an image from your system

docker rmi <image_id | image_name>
  • You must first remove (not just stop) any containers that are currently running off the image.

run

Starts a container from an existing image

docker run <image>
  • If you use a non-repository image, you should preface the name with the developer’s userid:
    • thomasr/myimage

Options

-d Detached
Runs the container in the background
-it

  • If the image does not exist locally, it will attempt to download it from the Docker repositories.  Once downloaded, subsequent ‘runs’ on that image will use the local copy.
  • New containers are automatically provided with a random Container Id and Name.

References

Ref:

stop

Stops a running container.

docker stop <container_id | container_name>
  • Does not remove the container from the system.  To remove it, you need to run docker rm …

Docker Labs

https://www.udemy.com/learn-docker/learn/lecture/15828562#overview

  • Recommended to use Chrome browser.
  • Lab on the left
    • Lab environments are deleted after an hour.
  • Quiz portal on the right.
    • if the quiz gets ‘stuck’, break it out from the lab side
    • if you skip a question, you cannot return to it.

Basic Docker Commands Lab

https://www.udemy.com/learn-docker/learn/lecture/15828568#overview

…start lesson 10

LEAVE A COMMENT