I'm using docker-compose.yml to setup docker containers. And I have started the services using docker-compose up -d.
Now every time I deploy the application to the server I need to restart one of the services.
Previously I used to run the container without docker-compose using just the docker run command like this: docker run --name test-mvn -v "$(pwd)":/usr/src/app test/mvn-spring-boot -d.
And to restart the container I used to do docker restart test-mvn.
But now there are two options out there docker-compose restart and docker restart. I'm not sure which one I should prefer.
I want to know what is the difference between these two options and which one I should use in my case.
With docker-compose you manage a services, typically constituting multiple containers, while docker manages individual containers. Thus docker-compose restart will restart all the containers of a service and docker restart only the given containers.
Assuming "one of the services" in your question refers to an individual container I would suggest docker restart.
Related
I am running a docker service in swarm mode. When I want to restart it, there are 2 options I know:
from swarm manager: docker service scale myservice=0 then docker service scale myservice=1
from server running the server: docker ps, take the container id of my service and do docker stop <containerId>
And this works fine. However, if I go with option #2 and instead of docker stop I write docker restart it will restart the current instance, but because being in swarm mode it will also start a new one. So in the end I will end up having 2 of the same service, even though in my compose I have specified I want only 1 replica.
Is there any way to prevent docker restart and the docker swarm to start a 2nd service while one is already there?
I am using docker 18.09.2 on ubuntu 18.04
I'm running three containers consisting of mongo, express, and react using docker-compose.
While I run them by docker-compose, what I need to watch is only the output of express container. So I want to run the mongo container in detach mode.
I'm aware of there is a command to run docker-compose in detach mode, docker-compose up -d
However, what I want to do is not to run whole containers in detach mode, but to run some specific containers in detach mode.
Is there any way to handle this?
yes you can by using the service name as a parmaeter
docker-compose up -d service1
docker-compose up -d service2
docker-compose up service3
You can start your distributed application in detached mode with
docker-compose up -d
and follow only the logs that you are interested in with
docker-compose logs -f express
This way any dependencies defined in the docker-compose.yml e.g. dependencies services have to one other are maintained and containers are started in an appropriate order.
I am very new to docker , just started venturing into this. I read online about this. I came to know of the following commands of docker which is: docker run and docker service. As I understood , with docker run we are spinning a new container. However I am not clear what docker service do? Does it spin container in a Swarm?
Can anyone help understand in simple to understand?
The docker run command creates and starts a container on the local docker host.
A docker "service" is one or more containers with the same configuration running under docker's swarm mode. It's similar to docker run in that you spin up a container. The difference is that you now have orchestration. That orchestration restarts your container if it stops, finds the appropriate node to run the container on based on your constraints, scale your service up or down, allows you to use the mesh networking and a VIP to discover your service, and perform rolling updates to minimize the risk of an outage during a change to your running application.
Docker Run vs Docker service
docker run:
we can create number of containers with different images.
docker service:
we can create number of containers with same image in a single command line.
SYNTAX:
docker service create --name service-name --network network-name --replicas number-of-containers image-name
EXAMPLE:
docker service create --name service1 --network swarm-net --replicas 5 redis
Why does docker compose create containers that are only accecible from docker-compose ps and that persist after killing running container ?
It doesn't.
docker ps only shows running containers, docker-compose ps shows all containers related to the current compose file, running and stopped. docker-compose kill just force stops the container and it can be restarted with docker-compose start, it will therefore be visible when running docker-compose ps but not docker ps.
To list all containers with docker use docker ps -a. To removed stopped containers related to a compose file run docker-compose rm, if you want to stop and remove all containers, have a look at docker-compose down.
docker-compose is the software wrapper around docker and there is not full support still. You can try to read a little bit about kubernets and mesos for the comparing different clusters built on top of docker or similar container systems.
article on blog about swarm and compose
This is a two-part question.
First part:
What is the best approach to run Consul and a Tomcat in the same docker container?
I've built my own image, installing both Tomcat and Consul correctly, but I am not sure on how to start them. I tried putting both calls as CMD in the Dockerfile, but no success. I tried to put Consul as an ENTRYPOINT (Dockerfile) and Tomcat to be called in the "docker run" command. It could be vice versa but I have a feeling that it is no good way either.
The docker will run in one AWS instance. Each docker container would run Consul as a server, to register themselves in another AWS instance. Consul and Consul-template will be integrated into proper load balance. This way, my HAproxy instance will be able to correctly forward the requests as I plug or unplug containers.
Second part:
In individual tests I did, the docker container was able to reach my main Consul server(leader) but it failed to register itself as an "alive" node.
Reading the logs at Consul server, I think is a matter of which ports I am exposing and publishing. In AWS, I already allowed communication in all ports in TCP and UDP between the instances in this particular Security Group.
Do you know which ports I should be exposing and publishing to allow proper communication between a standalone consul(aws instance) and consul servers (running inside docker containers inside a aws container)? What is command to run the docker container: docker run -p 8300:8300 .........
Thank you.
I would use ENTRYPOINT to kick off a script on docker run.
Something like
ENTRYPOINT myamazingbashscript.sh
Syntax might be off but u get the idea
The script should start both services and finally should tail -f the tomcat logs (or any logs).
tail -f will prevent container exit since the tail -f command never exits and it will also help u to see what tomcat is doing
Do ... docker logs -f to watch the logs after a docker run
Note because the container doesn't exit u can exec into it with ... docker exec -it containerName bash
This lets you have a sniff around inside the container
Not generally the best approach to have two services in one container because it destroys the separation of concerns and reusability but u may have valid reasons
To build use docker build then run with docker run as u stated it.
If u decided to go for a 2 container solution then u will need to expose ports between containers to allow them to talk to each other. You could share files between containers using volumes_from