Docker delete container after running

When you run a docker image, it create a container and run it. After docker container stop, container stays, so you can use it again if required.

Some times, you need to delete docker container after it is run. This is useful if your docker container is just a command line executable. In my case, docker container have ffmpeg in it, i don’t want container left over after i executed ffmpeg command.

To auto delete container after execution, use –rm option.

Example

Run PostgreSQL in Docker

To run PostgreSQL on docker, create a directory for saving the data presistant

run docker container

In above, change the value for POSTGRES_DB, POSTGRES_USER and POSTGRES_PASSWORD as needed.

Connect to PostgreSQL server

To connect to PostgreSQL server, run

Now you are inside PostgreSQL conainer, to login, run

It will ask for password. Once you enter password, you will be in PostgreSQL command line.

See docker

Docker compose start container on boot

I have a docker container, that i need to start on server boot.

The docker-compose.yml file i used to create this docker container is

With above docker-compose.yml file. i have to start docker container manually after server reboot.

To make it auto start, add the line

Here is the modified docker-compose.yml file.

You need to rebuild docker container based on this docker-compose.yml file

Change to the folder where your docker-compse.yml file is, in my case it was

Run

docker-compose: error while loading shared libraries

When running docker-compose on CentOS 7, i get following error

To fix the error, do the following

Now create a new file

Add following content

Make it executable

Create temp folder

Now docker-compose will work.

python-flask-docker

Create Python Flask Docker Container

Create a folder and change to the folder

Create file

Add following content

Create file requirements.txt, add “Flask” to it.

Now lets create our Python Flask Application

Add following content

To test Flask application locally, install Flask using pip

Now run the application using

Now you will be able to see web application at

Press CTRL+C to stop the application.

To build Docker image, run

-t specify tag.

If all worked properly, you will see

You can see the container image listed

Now your docker image is ready. To build a container using this image, run

You can access your python application running inside docker container on URL

To stop the application, run

To start it, run

Docker Nginx Proxy

Docker Nginx Proxy allow you to run multiple docker containers on same server behind nginx proxy. This is done using

https://github.com/jwilder/nginx-proxy

To do this, you need a server with port 80 and 443 unused.

To setup nginx proxy, run following

This will start nginx proxy. You can modify .env file if you want.

Starting a Docker Web App Behind Proxy

To start a web app, all you need is to start docker container on same network as nginx proxy. By default it is “webproxy”.

Here is an example command to start a web server.

This will start a test web server. You need to point the domain specified to this servers IP, then only nginx proxy can get LetsEncrypt SSL installed.

Replace test.serverok.in with your actual domain.

If you don’t want LetsEncrypt SSL installed, you can remove following 2 options

aws-elastic-beanstalk-2

Deploy Docker Image using Elastic Beanstalk

First create a file docker-eb-run.json with following content

here i used docker container bitnami/tomcat, you can use any container.

Login to AWS Console, go to AWS Elastic Beanstalk page. Click Get Started.

On next page, it ask for

For Application code, select Upload your code, click upload button and select “docker-eb-run.json” file you created.

Click “Create application” button. AWS will start deploying your docker container in Elastic Beanstalk, it will take a few minutes to complete.

Once deployment is completed, you get URL like

aws

Docker Delete all images

Before you can delete a docker image, you need to delete any container that is based on this image. So see how to delete all docker containers, see

Delete all docker containers

To list all available docker images, run

To just display image ID only, run with -q option

Now lets pass the result to docker rmi command using xargs

This will delete all images. You can also use

See docker