Docker Basic Commands

0
1. Launching a container : 

docker run <image-name>
e.g docker run ubuntu

2. Checking the list of docker containers:

For running containers :  docker ps 

For running & shutdown list : docker ps -a 

3. Stopping the docker container (below command just stops the container it doesn't have files created by container ) 

docker stop <container id / container_name > 
e.g docker stop 0uy1212121d19839121

4. Removing the stopped container

docker rm  <container id / container_name > 
e.g docker rm smart_world

Note: Its possible to stop multiple containers at the same time either by container ids / container name. While using container id, it is not necessary to specify the enter id , its enough to specify 3/4 characters of container id

docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
fa7d61b970f0        ubuntu              "sleep 20"          11 minutes ago      Exited (0) 11 minutes ago                       peaceful_boyd
aa45e8d98ee9        ubuntu              "/bin/bash"         12 minutes ago      Exited (0) 12 minutes ago                       friendly_engelbart

$ docker rm fa7 aa4
fa7
aa4

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

5.  Removing the docker image

docker rmi <image name > 

6. Downloading Image 

docker pull <image name >

7. Launching the container based on the download the image 

docker exec <image name> 

8. Append (Detach / Attach ) 

To run in background : 
docker run -d <image name> additional command ifany 
e.g docker run -d centos sleep 200

To bring the container to foregroung 

docker attach <conatiner-id>  

9. Accessing the bash of container interactively 

docker run -it <image-id> bash
e.g docker run -it ubuntu bash 

Things to remember: 

Port Mapping:

By default , containers get launched with network layer. Applications installed in the container can be accessible by two methods. 

1. IP address of the container and by this way. only docker host can access the applications .
2. IP address of the docker host .  This is possible by only if port mapping parameter is mentioned while launching the container
    docker run -p <dockerhost-portnumber>:<dockerapplication-portnumber> <image-id>
    e.g docker run -p 8090:8080 jenkins

Volume Mapping:

By default , data stored in containers are available only when it is running. ie. if a container is stopped , all the data stored will get deleted. In order to persist the storage of container applications and its required to specify the volume mapping parameter when launching the container. 

  docker run -v <location of docker host>:<location of container> <image id> 
  e.g docker run -p 3306:3306 -v /opt/mysqldata:/var/mysql  mysql

docker file and build is used to create custom docker image as per the requirements. 

Note:  docker file should always begin with os related image . 

sample docker file 



Building the docker 

docker build . 

docker build . -t mybicommunityapp 

Pushing the code is a two step process. docker login & docker push 

Docker Compose :  (docker-compose.yml)

Compose file is a YAML file defining services, networks and volumes and which helps to launch stack of containers 



launching the container using docker- compose file 

docker-compose up 

Docker network : 

3 options . 




Docker Swarm : is the concept of clustering of containers to avoid single point of failure .