Tuesday, September 11, 2018

Image,Registry and Container

Image: Binaries,libraries and source code all made that our application.
Image is the application we want to run.

To see existing images locally.
docker image ls

Container: Running instance of that image running as a process.We can have many containers running off the same image.

Registries: Repositories for container images.
Docker default image registry: hub.docker.com

To start a container.:
(old way)     docker run
(new way)    docker container run

docker container run --publish 80:80 nginx

Note: Everytime a new container is created,when we run this command.And it adds a random name to it.To name it our own use --name switch.

 Process of above command:
  1. Looks for that image locally in image cache,doesn't find anything.
  2. Then looks in remote image repository(defaults to Docker Hub)
  3. Downloads the latest version(nginx:latest by default)
  4. Creates new container based on that image and prepares to start
  5. Gives it a virtual IP on a private network inside docker engine.
  6. Opens up port 80 on host and forwards to port 80 in container.
  7. Starts container by using the CMD in the image Dockerfile.

The Docker engine looks for an image called nginx and pull down the latest image for nginx from Docker Hub.Then it started it as a new process in a new container for us to use.

The publish part exposes my local port 80 on my local machine and sends all traffic from it to the executable running inside that container on port 80.

And since Nginx is a webserver , it's going to default to port 80 and the traffic just forwards automatically through my browser to my local host and into that container.



After the image downloaded and container created, on the host machine web browser, type http://localhost

To stop the console output, press ctrl+c..It also stops the container running.

docker container run --publish 80:80 --detach nginx 


This will run the container runs in background and it shows a unique ID of container.
You can use --detach or -d

docker container ls  (old way: docker ps)

To list our containers running on the machine.
To show all containers, use -a switch



docker container stop <first few digits of ID> (old way: docker stop)

ID or name can be provided for stop, for multiple containers, separate them by comma.

The output of the background containers won't be visible.So use below command to fetch them when we need.

docker container logs <container_name> (old way: docker logs)


To see the processes running inside a container.:
docker container top <container_name>

You can see for nginx, there are 2 processes(master and worker.).

docker container --help
  (for more commands)


To remove a container:
docker container rm <one or more contianer ids typed partially or completely> 


(old way: docker rm)
 

To delete a running container,use -f switch.

No comments:

Post a Comment