I'm unable to start Tomcat server from docker compose.
When I log into container using docker exec -it <container id> bash and see ps -eaf | grep "tomcat" it is showing empty. Tomcat server is not running.
docker-compose.yml file:
version: "3"
services:
meghcore:
build: ./Core
container_name: 'meghcore'
expose:
- '8080'
ports:
- '8080:8080'
volumes:
- meghcore:/opt/Tomcat1/webapps/
command: /bin/bash
tty: true
stdin_open: true
networks:
- meghnet
volumes:
meghcore:
networks:
meghnet:
driver: bridge
Dockerfile file:
FROM tomcat:8.5.35
WORKDIR /app
COPY . /app
RUN mv /app/*.war /opt/Tomcat1/webapps/
ENV PATH $PATH:/opt/Tomcat1/bin
WORKDIR /opt/Tomcat1/bin
EXPOSE 8080
CMD ["catalina.sh", "run"]
Since you specify an alternate command: in your docker-compose.yml file, that overrides the CMD in the Dockerfile. You don't need most of the options you specify there at all, and several of them (the alternate command:, the volumes: overwriting the actual application) interfere with the normal container operation.
A complete, functional docker-compose.yml would be
version: "3"
services:
meghcore:
build: ./Core
ports:
- '8080:8080'
None of the other options you list out are necessary. If there were other containers listed in the file, they could still communicate using their Docker Compose service names, without any special setup (another container in this same file could successfully call http://meghcore:8080).
What is happening is command specify in docker-compose.yml is overwriting the CMD provided in dockerfile.
kindly update command with the command available in dockerfile or remove command from docker-compose.yml
Problem is resolved by adding below commands in dockerfile and removed command from docker compose file.
ENV PATH $PATH:$JAVA_HOME/bin
ENV PATH $PATH:/opt/Tomcat1/bin
WORKDIR /opt/Tomcat1/bin
EXPOSE 8080
CMD ["catalina.sh", "run"]
Related
I created a docker-compose file to build image from dockerfile and then run container this is my code:
Dockerfile
FROM anapsix/alpine-java
VOLUME [ "/var/run/jars/" ]
ADD hello-world.jar /var/run/jars/
EXPOSE 8080
ENTRYPOINT [ "java" ]
CMD ["-?"]
docker-compose.yml
version: '3'
services:
hello-world-image:
build: .
image: hello-world-image
hello-world:
image: hello-world-image
container_name: hello-world
ports:
- "8080:8080"
volumes:
- ./logs_ACM:/root/logs_ACM
command: -jar /var/run/jars/hello-world.jar
restart: always
docker ps output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
103b0a3c30e3 hello-world-image "java -jar /var/run/…" 5 seconds ago Restarting (1) Less than a second ago hello-world
When i check running containers with "docker ps" the port column is empty therefore no port mapping was done even though i specified ports in my docker compose file.
What changes needed to be done on my docker-compose file to solve this issue ?
new version of dockerfile and docker-compose :
FROM anapsix/alpine-java
USER root
RUN mkdir -p /var/run/jars/
COPY spring-petclinic-2.4.2.jar /var/run/jars/
EXPOSE 8081
ENTRYPOINT [ "java" ]
CMD ["-?"]
version: '3' # '3' means '3.0'
services:
spring-petclinic:
build: .
# Only if you're planning to `docker-compose push`
# image: registry.example.com/name/hello-world-image:${TAG:-latest}
ports:
- "8081:8081"
volumes:
# A bind-mount directory to read out log files is a good use of
# `volumes:`. This does not require special setup in the Dockerfile.
- ./logs_ACM:/root/logs_ACM
command: -jar /var/run/jars/spring-petclinic-2.4.2.jar
mysql:
image: mysql:5.7
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=
- MYSQL_ALLOW_EMPTY_PASSWORD=true
- MYSQL_USER=petclinic
- MYSQL_PASSWORD=petclinic
- MYSQL_DATABASE=petclinic
volumes:
- "./conf.d:/etc/mysql/conf.d:ro"
I think your single biggest problem here is the VOLUME directive in the Dockerfile. The Dockerfile documentation for VOLUME notes:
Changing the volume from within the Dockerfile: If any build steps change the data within the volume after it has been declared, those changes will be discarded.
So when you declare a VOLUME for the directory containing the jar file, and then try to ADD content to it, it gets lost.
In most practical cases you don't need a VOLUME. You should be able to rewrite the Dockerfile to:
FROM anapsix/alpine-java
# Do not create a VOLUME.
# Generally prefer COPY to ADD. Will create the target directory if needed.
COPY hello-world.jar /var/run/jars/
EXPOSE 8080
# Don't set an ENTRYPOINT just naming an interpreter.
# Do make the default container command be to run the application.
CMD ["java", "-jar", "/var/run/jars/hello-world.jar"]
In the docker-compose.yml file, you don't need a separate "service" just to build the image, and you shouldn't typically need to override container_name: (provided by Compose) or command: (from the Dockerfile). This could be reduced to:
version: '3.8' # '3' means '3.0'
services:
hello-world:
build: .
# Only if you're planning to `docker-compose push`
# image: registry.example.com/name/hello-world-image:${TAG:-latest}
ports:
- "8080:8080"
volumes:
# A bind-mount directory to read out log files is a good use of
# `volumes:`. This does not require special setup in the Dockerfile.
- ./logs_ACM:/root/logs_ACM
# Don't enable auto-restart until you've debugged the start sequence
# restart: always
I am trying to start a docker container using a redis db that I have a persistent copy saved to a local machine.
I currently have a docker container loading redis with a volume using this docker-compose.yml but it misses my redis.conf (which contains the loadmodule command) is located in the volume with the rdb file
version: '3'
services:
redis:
image: redis
container_name: "redis"
ports:
- "6379:6379"
volumes:
- E:\redis_backup_conf:/data
This begins to load the RDB but crashes out because the data uses this time series module.
I can load a seperate docker container with a fresh redis db that has the time seriese module loaded using the following dockerfile. My issue is I can't figure out how to do both at the same time!
Is there someway of calling a dockerfile from a docker-compose.yml or declaring the volume in the dockerfile?
That, or should I be creating my own image that I can call in the docker-compose.yml?
Any help woule be appreciated, I'm honeslty just going round in circles I think.
dockerfile
# BUILD redisfab/redistimeseries:${VERSION}-${ARCH}-${OSNICK}
ARG REDIS_VER=6.0.1
# stretch|bionic|buster
ARG OSNICK=buster
# ARCH=x64|arm64v8|arm32v7
ARG ARCH=x64
#----------------------------------------------------------------------------------------------
FROM redisfab/redis:${REDIS_VER}-${ARCH}-${OSNICK} AS builder
ARG REDIS_VER
ADD ./ /build
WORKDIR /build
RUN ./deps/readies/bin/getpy2
RUN ./system-setup.py
RUN make fetch
RUN make build
#----------------------------------------------------------------------------------------------
FROM redisfab/redis:${REDIS_VER}-${ARCH}-${OSNICK}
ARG REDIS_VER
ENV LIBDIR /usr/lib/redis/modules
WORKDIR /data
RUN mkdir -p "$LIBDIR"
COPY --from=builder /build/bin/redistimeseries.so "$LIBDIR"
EXPOSE 6379
CMD ["redis-server", "--loadmodule", "/usr/lib/redis/modules/redistimeseries.so"]
EDIT:
ok.. slight improvement i can call a redis-timeseries image in the docker-compose.yml
services:
redis:
image: redislabs/redistimeseries
container_name: "redis"
ports:
- "6379:6379"
volumes:
- E:\redis_backup_conf:/data
This is a start however I still need to increase the maximum number of db's, I have been using the redis.conf to do this in the past.
You can just have docker-compose build your dockerfile directly. Assume your docker-compose file is in folder called myproject . Also assume your dockerfile is in a folder called myredis and that myredis is in the myproject folder. Then you can replace this line in your docker-compose file:
Image: redis
With:
Build: ./myredis
That will build and use your custom image
I am using Docker which is running fine.
I can start a Docker image using docker-compose.
docker-compose rm nodejs; docker-compose rm db; docker-compose up --build
I attached a shell to the Docker container using
docker exec -it nodejs_nodejs_1 bash
I can view files inside the container
(inside container)
cat server.js
Now when I edit the server.js file inside the host, I would like the file inside the container to change without having to restart Docker.
I have tried to add volumes to the docker-compose.yml file or to the Dockerfile, but somehow I cannot get it to work.
(Dockerfile, not working)
FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
VOLUMES ["/usr/src/app"]
EXPOSE 8080
CMD [ "npm", "run", "watch" ]
or
(docker-compose.yml, not working)
version: "3.3"
services:
nodejs:
build: ./nodejs-server
ports:
- "8001:8080"
links:
- db:db
env_file:
- ./.env-example
volumes:
- src: /usr/src/app
db:
build: ./mysql-server
volumes:
- ./mysql-server/data:/docker-entrypoint-initdb.d #A folder /mysql-server/data with a .sql file needs to exist
env_file:
- ./.env-example
volumes:
src:
There is probably a simple guide somewhere, but I havn't found it yet.
If you want a copy of the files to be visible in the container, use a bind mount volume (aka host volume) instead of a named volume.
Assuming your docker-compose.yml file is in the root directory of the location that you want in /usr/src/app, then you can change your docker-compose.yml as follows:
version: "3.3"
services:
nodejs:
build: ./nodejs-server
ports:
- "8001:8080"
links:
- db:db
env_file:
- ./.env-example
volumes:
- .:/usr/src/app
db:
build: ./mysql-server
volumes:
- ./mysql-server/data:/docker-entrypoint-initdb.d #A folder /mysql-server/data with a .sql file needs to exist
env_file:
- ./.env-example
I have two docker containers: simple rest api and a database. I want to start the rest api only when the database is ready. I tried several solutions, when i figured out the problem is in the network.
wait-for-it.sh script works perfectly fine when I start rest api without waiting for the database and then do docker exec -it <api-container-name> bash and run it from there. When I'm trying to run it as a CMD in Dockerfile it can't establish connection with the database. Same thing happens when I start the api while database is already running.
Api dockerfile:
FROM microsoft/aspnetcore-build:2.0
WORKDIR /app
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN chmod +x Scripts/wait-for-it.sh
RUN Scripts/wait-for-it.sh -t 30 172.20.1.2:3306 #times out when waiting for database
RUN dotnet publish -c Release -o out
ENTRYPOINT ["dotnet", "out/Atlanta.dll"]
docker-compose:
version: '3'
networks:
backend:
driver: bridge
ipam:
config:
- subnet: 172.20.1.0/24
services:
main-db:
container_name: main-db
image: mysql
environment:
MYSQL_DATABASE: Main
MYSQL_ROOT_PASSWORD: root
ports:
- "5000:3306"
networks:
backend:
ipv4_address: 172.20.1.2
atlanta-ms:
container_name: atlanta
build:
context: ./Atlanta
dockerfile: Dockerfile
image: atlanta:ms
ports:
- "5001:80"
networks:
backend:
ipv4_address: 172.20.1.3
I see your confusion.
The RUN statement isn't doing what you think it is; it's running the wait-for-it.sh while the container is building, and isn't under the docker-compose's control. It will not run when your container runs! You should check out the documentation from docker about container start-up order and docker-compose.
Detached mode for your database will have no effect on build/run process; expect you won't be able to interact with it.
Using docker-compose will, by default, have all the container in non-interactive mode; but that's okay because you can still attach/detach to the containers.
You should add a depends-on option to your docker-compose.yml and add your wait-for-it.sh to the command option in the docker-compose.yml, not the Dockerfile.
version: '3'
networks:
backend:
driver: bridge
ipam:
config:
- subnet: 172.20.1.0/24
services:
main-db:
container_name: main-db
image: mysql
environment:
MYSQL_DATABASE: Main
MYSQL_ROOT_PASSWORD: root
ports:
- "5000:3306"
networks:
backend:
ipv4_address: 172.20.1.2
atlanta-ms:
container_name: atlanta
build:
context: ./Atlanta
dockerfile: Dockerfile
# Add this `depends-on`
depends-on:
- "main-db"
# Add this `command` option
command: ["Scripts/wait-for-it.sh", "-t", "30", "172.20.1.2:3306"]
image: atlanta:ms
ports:
- "5001:80"
networks:
backend:
ipv4_address: 172.20.1.3
I would recommend moving wait-for-it.sh to your WORKDIR, and make sure it is passed as "./wait-for-it.sh" to command, just to make your life easier.
Don't forget to remove RUN Scripts/wait-for-it.sh -t 30 172.20.1.2:3306 from your Dockerfile! (Because docker-compose is handling it now.)
And remember that the command for using docker-compose is docker-compose up, unless, that is, you'd like to use docker swarm instead.
Well. I managed to solve my problem. I can guess this is not a clean way of doing it, but it works for now.
I'm building and running both containers first. Then, when both api and db are up, i'm remotely executing migration commands using docker exec. wait-for-it script is not needed here.
run.sh
#!/bin/bash
docker-compose up -d --build atlanta-ms main-db
docker exec atlanta bash -c "dotnet ef migrations add InitialMigration && dotnet ef database update"
rest api Dockerfile
FROM microsoft/aspnetcore-build:2.0
WORKDIR /app
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
ENTRYPOINT ["dotnet", "out/Atlanta.dll"]
docker-compose.yml
version: '3'
networks:
backend:
driver: bridge
ipam:
config:
- subnet: 172.20.1.0/24
services:
main-db:
container_name: main-db
image: mysql
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "5000:3306"
networks:
backend:
ipv4_address: 172.20.1.2
atlanta-ms:
container_name: atlanta
build:
context: ./Atlanta
dockerfile: Dockerfile
image: atlanta:ms
ports:
- "5001:80"
networks:
backend:
ipv4_address: 172.20.1.3
The drawback of this solution is that rest api is running in detached mode, so i cannot stop it just by doing ctrl + c in console. Adding docker attach atlanta line at the end of run.sh script works (i can stop container by simple ctrl + c), but it doesn't work with several containers (cannot attach to more than one container) so i have to write simple stop script and call it independently from run.sh to stop containers, what is a little inconvenient.
I would be very grateful if someone could tell me how can i attach to few containers, so i could stop them all with ctrl + c (thats how the docker-compose up service1 service2 service3 without "-d" flag works)
I have this folder structure:
/home/me/composetest
/home/me/composetest/mywildflyimage
Inside composites I have this docker-compose.yml:
web:
image: test/mywildfly
container_name: wildfly
ports:
- "8080:8080"
- "9990:9990"
Inside mywildflyimage I have this docker image:
FROM jboss/wildfly
EXPOSE 8080 9990
ADD standalone.xml /opt/jboss/wildfly/standalone/configuration/
RUN /opt/jboss/wildfly/bin/add-user.sh admin admin --silent
CMD ["/opt/jboss/wildfly/bin/standalone.sh", "-b", "0.0.0.0", "-bmanagement", "0.0.0.0"]
If i run
docker built -t test/mywildfly .
docker-compose up
Everything works great, and the management part is minded to 0.0.0.0 (-bmanagement 0.0.0.0 part of the CMD command).
If I change my docker-compose.yml:
web:
build: mywildflyimage
container_name: wildfly
ports:
- "8080:8080"
- "9990:9990"
and run
docker-compose up
It still boots, but the admin part is not bound to 0.0.0.0 anymore (this is the default behaviour for the image I inherited from).
Why does it stop working when I use the build command in the docker-compose.ml?
EDIT: It seems that it is ignoring all my docker file commands.
run docker-compose build after changing docker-comopse.yml and then docker-compose up
Before you type docker-compose up, you should build images with docker-compose build [options] [SERVICE...].
Options:
--force-rm Always remove intermediate containers.
--no-cache Do not use cache when building the image.
--pull Always attempt to pull a newer version of the image.
In your case, ex: docker-compose build --no-cache web