Is a way of deploying code and dependencies as a single unit.
The official docker definition of container is:
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.
- The application and its dependencies are bundled
- Differences between development and production environments are minimised
- The application defines the lifespan of the container
- Share a low level OS isolating the application
- Containers can be run in parallel which allows horizontal scaling to improve performance
- Containers are lightweight
While there are many different container platforms, currently docker is the most popular in the industry.
Docker is available for Linux, Mac Os and Windows (as well as cloud versions for AWS, Azure and IBM Cloud), the installers can be found here: https://docs.docker.com/v17.12/install/
Docker Engine: it is the software that runs Docker locally and it is made of e layers: Daemon, API and Client.
- Daemon: manages the images, containers, networks and volumes
- API: The client communicates with the daemon through the API (command line)
- Client: is what you will use to interact with docker, and it is accessed using the Docker Command
Docker Image: Contains all of the instructions to create a docker container, multiple containers can be created from an image.
Docker Container: is a runnable instance of an image, the containers are not designed for persistent data
Docker Registry: this is where images are stored, these can be public or private, for example Docker Hub, docker images can be distributed using a docker registry
Volumes: Since containers do not persist data, we can create and attach Volumes to containers to store and share data across containers
1- Pull the image
docker pull postgres:latest
2- Run the image
docker run --name psql -e POSTGRES_PASSWORD=your-password -p 5432:5432 -d postgres:latest
–name is to indicate the name we want to assign to the container
-e is to set an environment variable in this case the POSTGRESS_PASSWORD value to your-password
-p is to bind the local machine port to the container port so when using the port 5432 on the local machine we can access the same port on the container
-d is to detach the terminal from the running image, so we can use the same terminal, otherwise we will need to open a new terminal to keep on working
The last part of the command indicates the image we want to run (postgres:latest)
2. How AI Will Power the Next Wave of Healthcare Innovation?
3. Machine Learning by Using Regression Model
4. Top Data Science Platforms in 2021 Other than Kaggle
3- We can now connect to the running service on the container (assuming we have posgres client installed locally)
psql -h 0.0.0.0 -p 5432 -U postgres
4- To list the current running containers execute:
5- To stop a running container you will need the container_id which we can get by listing them with the command mentioned above, to stop the container just run:
docker stop <container_id>
Dockerfiles are there to define Docker images, a docker image will typically be defined from a base image, upon which more layers can be added on top to create a custom image that fits our needs, to define the base (source) image the FROM keyword is used, a simple example of a docker file below:
FROM python:3.7.2-slimCOPY . /application
WORKDIR /applicaitonRUN pip install --upgrade pip
RUN pip install flask
ENTRYPOINT [“python”, “app.py”]
- This image will use the python:3.7.2-slim image as base,
- Will copy all files from the ./application folder on the local machine to the container,
- Will set the /application folder as the working directory inside the container
- Will upgrade pip to the latest version
- Will install flask on top of the python:3.7.2 image files
- And when the container is started will launch app.py automatically
#: to define comments
FROM: to define the source image
COPY: to copy files from the local computer to the container
WORKDIR: to define the working directory in the container
RUN: to run commands
ENTRYPOINT: to define the main executable when the container is started
You can find the official Dockerfile Reference here
Once we have the dockerfile created we can now build an image from it with the following command:
docker build --tag helloworld .
This will build and image called helloworld
Important: note that the command ends with a period ‘.’
that is very important, it tells docker to use the Dockerfile in the current directory
Once the image was built we just need to run the image:
docker run helloworld
This will run a container from the docker image we just created.
And that’s it a very quick introduction on how to use docker.
Original post: https://www.georgestudenko.com/software-craftsmanship/docker-containers-what-are-they-how-to-create-and-use-them/