How to install Docker?
-
Sign up for Docker Hub: https://hub.docker.com/signup
-
Navigate to https://hub.docker.com/?overlay=onboarding
-
Download *Docker Desktop for Mac* or *Docker Desktop for Windows*.
-
Install Docker Desktop.
Try Play-With-Docker online tool
Instead of installing Docker on your machine, you can try online version of Play-With-Docker
for learning purpose.
Play-With-Docker, which is a website where you can run terminals directly from your browser that have Docker installed.
What is a Container?
A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.
Containers are an abstraction at the app layer that packages code and dependencies together. Multiple containers can run on the same machine and share the OS kernel with other containers, each running as isolated processes in user space. Containers take up less space than VMs (container images are typically tens of MBs in size), can handle more applications and require fewer VMs and Operating systems.
How to run a Container?
- Start Play-With-Docker online tool and click on
Add New Instance
button.
Run below command in the terminal window.
|
|
You use the docker container run
command to run a container with the Ubuntu image by using the top
command. The -t
flag allocates a pseudo-TTY, which you need for the top
command to work correctly.
The docker run
command first starts a docker pull
to download the Ubuntu image onto your host. After it is downloaded, it will start the container.
2. Add a new node by clicking Add New Instance
and then ssh from node2 into node1 by using the IP that is listed by node1, for example:
|
|
3. Clear the terminal by typing clear
command and get the ID of the running container that you just created.
|
|
4. Use that container ID to run bash
inside that container by using the docker container exec
command. Because you are using bash and want to interact with this container from your terminal, use the -it
flag to run using interactive mode while allocating a psuedo-terminal:
|
|
Using docker container exec
with bash
is a common way to inspect a Docker container.
5. Get all running processes using ps
command.
|
|
6. You can now clean up the container running the top
processes:
|
|
How to run multiple containers?
1. Docker Store
The Docker Store is the public central registry for Docker images. Anyone can share images here publicly. The Docker Store contains community and official images that can also be found on the Docker Hub.
When searching for images, you will find filters for Store and Community images. Store images include content that has been verified and scanned for security vulnerabilities by Docker. Go one step further and search for Certified images that are deemed enterprise-ready and are tested with Docker Enterprise Edition.
2. Run NGINX container
Run an NGINX server by using the official NGINX image from the Docker Store in a new node instance window.
|
|
The --detach
flag will run this container in the background.
The publish
flag publishes port 80 in the container (the default port for NGINX) by using port 8080 on your host.
--name
flag, which names the container
3. Access NGINX server from browser by using http://localhost:8080
port.
Once you clicked on highlighted button, it will open up an tab in browser with NGINX url.
4. Run MongoDB container
Create a new instance and run a MongoDB server. You will use the official MongoDB image from the Docker Store. Instead of using the latest
tag (which is the default if no tag is specified).
|
|
Again, because this is the first time you are running a Mongo container, pull the Mongo image from the Docker Store. You use the --publish
flag to expose the 27017 Mongo port on your host. You must use a port other than 8080 for the host mapping because that port is already exposed on your host.
Now, click on 8081 button, and it will open up a tab in your browser window with following message.
Containers are self-contained and isolated, which means you can avoid potential conflicts between containers with different system or runtime dependencies. For example, you can deploy an app that uses Java 7 and another app that uses Java 8 on the same host. Or you can run multiple NGINX containers that all have port 80 as their default listening ports. (If you’re exposing on the host by using the --publish
flag, the ports selected for the host must be unique.) Isolation benefits are possible because of Linux namespaces.
Stop a container
1. Get all container list by running below command.
|
|
2. Stop container by their Ids.
|
|
|
|
Remove the stopped container
|
|
Remove the stopped containers. The following command removes any stopped containers, unused volumes and networks, and dangling images.