Skip to content

Commit

Permalink
Autostarting container with restart=always option
Browse files Browse the repository at this point in the history
Since I had problems in starting containers with systemd, I've decided
to restart containers using docker run option --restart=always
  • Loading branch information
bunop committed May 9, 2016
1 parent 647b084 commit 307df89
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 86 deletions.
39 changes: 2 additions & 37 deletions Services/KronaTools-Web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,14 @@

In this container image, Krona Tools 2.5 static files will be served by NGINX under /krona/ path. You have to start this container and then you can use this mirror by launching, for example, krona tools with `-u 'http://bioinformatics.tecnoparco.org/krona/'`

## Build the container and start it
## Build the container and start it (with autostart)

You may want to build a new container and run it on a defined port (in ordert to be reached from services likes nginx): Change your local directory to this directory (KronaTools-Web) and then:

```sh
$ docker build --rm -t ptp/web_krona_tools .
$ docker tag ptp/web_krona_tools:latest ptp/web_krona_tools:0.1
$ docker run -d -p 20080:80 --name web_krona_tools ptp/web_krona_tools
```

## Automatically starts Krona Tools container

[Here](https://docs.docker.com/articles/host_integration/#automatically-start-containers) you can find a useful documentation on how to start automatically a docker container with systemd. Create a new service file and the edit it:

```sh
$ touch /etc/systemd/system/web_krona_tools.service
$ chmod 664 /etc/systemd/system/web_krona_tools.service
```

Here is my service file:

```
[Unit]
Description=Web Krona Tools docker container
Requires=docker.service
After=docker.service
[Service]
Restart=always
ExecStart=/usr/bin/docker start -a web_krona_tools
ExecStop=/usr/bin/docker stop -t 2 web_krona_tools
[Install]
WantedBy=local.target
```

Now you have to reload service and start Krona Tools service:

```sh
$ systemctl daemon-reload
$ systemctl enable web_krona_tools
$ systemctl start web_krona_tools
$ systemctl status web_krona_tools
$ docker run -d -p 20080:80 --name web_krona_tools --restart=always ptp/web_krona_tools
```

## Add a NGINX stanza to reach this docker service
Expand Down
48 changes: 1 addition & 47 deletions Services/Rocker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Setting volume names facilitates process identification. Next start a new contai
```sh
$ docker run -d -p 8787:8787 -P --name rstudio --volumes-from rstudio_volume \
-v /etc/localtime:/etc/localtime:ro -v /mnt/dell_storage/storage/:/storage/ \
--cpuset-cpus=0-8 --memory="8G" ptp/rstudio
--cpuset-cpus=0-8 --memory="8G" --restart=always ptp/rstudio
```

the `-p` options set the standard rstudio port of the container on the host. It can be omitted with the `-P` options, and docker will bind the service on another port. The `-P` option is useful to export the openssh port of the container. With the `--cpuset-cpus` you can set a CPU interval in which the application can run. Unfortunately at the moment it seems impossible to set CPU dinamically. With the `--memory` option you can set the memory to allocate. You can specify a numeric value with a dimension like M for Mb, G for GB, and so on. More information about docker resource usage can be found [here](https://gist.github.com/afolarin/15d12a476e40c173bf5f).
Expand Down Expand Up @@ -84,52 +84,6 @@ $ docker exec -it rstudio /bin/bash
$ ssh -p <rstudio_ssh_port> root@localhost
```

## Automatically start Rstudio container

You can set the [--restart](https://docs.docker.com/reference/run/#restart-policies-restart) option during `docker run` instance to automatically restart a container. To automatically start a container during system startup, you can configure `systemd ` as described [here](https://docs.docker.com/articles/host_integration/#automatically-start-containers). [This](http://linuxaria.com/article/an-introduction-to-systemd-for-centos-7?lang=it) is a nice introduction to `systemd` phylosopy and [here](https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Managing_Services_with_systemd-Unit_Files.html) ther is the offical Centos 7 documentation on writing systemd configure file. Now start by create a custom file in `/etc/systemd/system/` directory and set the permissions

```sh
$ touch /etc/systemd/system/rstudio_server.service
$ chmod 664 /etc/systemd/system/rstudio_server.service
```
Now edit the file according to contaner name:

```conf
[Unit]
Description=RStudio server docker container
Requires=docker.service
After=docker.service
[Service]
Restart=always
ExecStart=/usr/bin/docker start -a rstudio
ExecStop=/usr/bin/docker stop -t 2 rstudio
[Install]
WantedBy=local.target
```

Now you have to reload all configuration for systemd, and start the service in order to get a systemd process id:

```sh
$ systemctl daemon-reload
$ systemctl enable rstudio_server
$ systemctl start rstudio_server
$ systemctl status rstudio_server

```

You may see a status like this:

```
rstudio_server.service - RStudio server docker container
Loaded: loaded (/etc/systemd/system/rstudio_server.service; disabled)
Active: active (running) since mer 2015-07-22 10:59:14 CEST; 2s ago
Main PID: 144397 (docker)
CGroup: /system.slice/rstudio_server.service
└─144397 /usr/bin/docker start -a rstudio
```

## Restart rstudio-server inside container

To restart rstudio server (for instance, after installing packages and libraries) simply do:
Expand Down
1 change: 0 additions & 1 deletion compose/postgis/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ db:
# Auto restart container
restart: always


# to export volume, as recommeded in https://registry.hub.docker.com/u/library/mysql/
volumes:
- "./pg-data:/var/lib/postgresql/data"
Expand Down
4 changes: 3 additions & 1 deletion compose/registry/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ registry:
restart: always
#ports:
# - 5000:5000
# environment:
environment:
# to delete local images: https://github.com/docker/docker-registry/issues/988
REGISTRY_STORAGE_DELETE_ENABLED: 'true'
# REGISTRY_HTTP_TLS_CERTIFICATE: /certs/domain.crt
# REGISTRY_HTTP_TLS_KEY: /certs/domain.key
# REGISTRY_AUTH: htpasswd
Expand Down

0 comments on commit 307df89

Please sign in to comment.